LigerUI --- use of ligerGrid paging sorting (obtain data display from the background ),
Preface:
The framework of the previous project uses ligerUI, which uses the ligerGrid table. The following describes how to obtain data from the background and perform a perfect display on the foreground page. Ah, haha .. (Oh, it seems that the sister-in-law sitting next to me can make hundreds of thousands of shares in a month. It's amazing ....)
1. Front-end Page code:
RoleGrid = $ ("# roleGrid "). ligerGrid ({columns: [{display: 'Role No. ', name: 'roleid', align: 'left', width: 100, minWidth: 60,}, {display: 'Role name', name: 'rolename', minWidth: 120}, // hide: 'true' {display: 'Role description', name: 'rolede', minWidth: 120 ,}, {display: 'Role type number', name: 'roletypeid', minWidth: 120 ,}, {display: 'Role type', name: 'roletypename ', minWidth: 120 ,}, {display: 'creator', name: 'creator', minWidth: 120 ,}, {display: 'creation time', name: 'ctime', minWidth: 120 },{ display: 'updater', name: 'updator', minWidth: 120 },{ display: 'Update time', name: 'utime', minWidth: 120 },{ display: 'System number', name: 'systemid', minWidth: 120 ,}, {display: 'System name', name: 'systemname', minWidth: 120 ,},], // The requested data does not need to be parsed. The table is automatically generated. Note that the fields must correspond to each other in the url '$ {baseURL}/listAllRole. action ', // sort the data submitted to the background by systemId sortName: 'systemid', // sort the data submitted to the background Sortorder: 'asc' in ascending order, // number of records displayed on each page of data submitted in the background pageSize: 10, // onSelectRow select row event onSelectRow: function (rowdata, rowindex, rowobj) {// select a row event... }, Checkbox: false, height: '20140901', rownumbers: true });
2. Background action code:
@ Controller ("RoleAction") public class RoleAction extends ActionSupport {// injection service layer interface @ Resource private RoleService roleService; public String roleId; public String roleName; public String roleDes; public String creator; public String updator; public String systemId; public String functionIds; public String cTime; public String roleTypeId; // receive the corresponding fields by page. // all the data submitted by the front-end to the background must be retrieved by yourself. // The public String pagesize of the current page is public String pagesize; // The size of each page is public String sortname; // The sorting field is public String sortorder; // whether it is in ascending or descending order // The get method private JSONObject jsonObject is provided for the returned json data; public JSONObject getJsonObject () {return jsonObject;} public String listAllRole () {try {// retrieve all the Role data. sort by page. Upload the relevant fields in the paging order to the dao layer for processing. Here, we will not explain more about the List <Role> roleList = roleService. getAllByPage (Role. class, Integer. parseInt (page), Integer. parseInt (pagesize), sortname, sortorder); // obtain the total number of records Integer totalCount = roleService. getCount (Role. class); List <RoleVO> roleVOList = new ArrayList <> (); // some data processing in this project is not required to be detailed here. for (int I = 0; I <roleList. size (); I ++) {RoleVO roleVO = new RoleVO (); roleVO. setRoleId (roleList. get (I ). getRoleId (); roleVO. setRoleName (roleList. get (I ). getRoleName (); roleVO. setRoleDes (roleList. get (I ). getRoleDes (); roleVO. setCreator (roleList. get (I ). getCreator (); roleVO. setUpdator (roleList. get (I ). getUpdator (); roleVO. setSystemId (roleList. get (I ). getSystem (). getSystemId (); roleVO. setSystemName (roleList. get (I ). getSystem (). getSystemName (); roleVO. setcTime (roleList. get (I ). getcTime (); roleVO. setuTime (roleList. get (I ). getuTime (); roleVO. setRoleTypeId (roleList. get (I ). getRoleType (). getRoleTypeId (); roleVO. setRoleTypeName (roleList. get (I ). getRoleType (). getRoleTypeName (); roleVOList. add (roleVO);} // because Rows are required for json-format data that has been required by ligerGrid, the Total field can be used to process map using the Map set. <String, object> map = new HashMap <> (); // Rows is used to record the data set map. put ("Rows", roleVOList); // Rows installs the total number of records map. put ("Total", totalCount); // JsonConfig jsonConfig = new JsonConfig (); jsonConfig. registerJsonValueProcessor (Date. class, new JsonDateValueProcessor (); jsonObject = JSONObject. fromObject (map, jsonConfig); java. lang. system. out. println ("jsonObject:" + jsonObject); // {"Total": 5, "Rows": [{"cTime": "00:00:00", "creator": 123, "roleDes": "test", "roleId": 33, "roleName": "Test 1", "roleTypeId": 2, "roleTypeName": "technology type ", "systemId": 1, "systemName": "USM", "uTime": "00:00:00", "updator": 123}] }} catch (Exception e) {e. printStackTrace ();} return SUCCESS ;}}
In fact, there is no big difference between paging sorting and the paging sorting we usually write. The only difference is that we now use the front-end framework to receive these related fields in the background and then upload them to the dao layer for processing.
Do not think that as long as the data returned in the background action is in json format, you can directly obtain the data on the foreground page and then display it. That is impossible.
The front-end can receive data in the json format in the background, which must be processed as follows:
1. first, the json format data returned by your background must correspond to the content of the fields displayed on the ligerGrid page on your foreground page (the Rows and Total of ligerGrid are the two fields required for data display on the foreground page, therefore, the background also uploads the json format data of these two fields ):
2. Configure the result set view returned by struts2 to set type = json (the struts2 framework is used here, so the struts2 framework is used as an example)
<action name="listAll*" class="{1}Action" method="listAll{1}"> <result type="json"> <param name="root">jsonObject</param> </result></action>
3. Set the package of the corresponding action to inherit from json-default.
<package name="fums" namespace="/" extends="struts-default,json-default">
4. Provide the get method for the data to be returned in the background action
5. Set the format of the returned data in struts. xml (see the code in step 1, that is, the <param> section. The returned json data name is
JsonObject, so the data source is filtered)
Succeeded ~~~~~~~~~~~~~~~~~~~~~~
You can also click the next page on the previous page for paging ..... It will not be displayed anymore .....