This article mainly introduces the sample code of jquerydatatable Data encapsulation in the background. For more information, see
1. Data Conversion
public class DataTableReturnObject { private int iTotalRecords; private int iTotalDisplayRecords; private String sEcho; private String[][] aaData; public DataTableReturnObject(int totalRecords, int totalDisplayRecords, String echo, String[][] d) { this.setiTotalRecords(totalRecords); this.setiTotalDisplayRecords(totalDisplayRecords); this.setsEcho(echo); this.setAaData(d); } public void setiTotalRecords(int iTotalRecords) { this.iTotalRecords = iTotalRecords; } public int getiTotalRecords() { return iTotalRecords; } public void setiTotalDisplayRecords(int iTotalDisplayRecords) { this.iTotalDisplayRecords = iTotalDisplayRecords; } public int getiTotalDisplayRecords() { return iTotalDisplayRecords; } public void setsEcho(String sEcho) { this.sEcho = sEcho; } public String getsEcho() { return sEcho; } public void setAaData(String[][] aaData) { this.aaData = aaData; } public String[][] getAaData() { return aaData; } }
2. Help class
public class BaseController { protected JSONResponse successed(Object obj) { JSONResponse ret = new JSONResponse(); ret.setSuccessed(true); ret.setReturnObject(obj); return ret; } }
3. Implementation class
Public JSONResponse searchList (HttpServletRequest request, HttpServletResponse response, String sEcho) throws Exception {// convertToMap is defined in the parent class and Adds all elements in the parameter array to a HashMap Map
ObjQueryMap = new HashMap
(); String jsondata = request. getParameter ("aoData"); JSONArray jsonarray = JSONArray. fromObject (jsondata); String strDisplayStart = ""; String strDisplayLength = ""; String [] arrayColumen = new String [new JSONUser (). toArray (). length]; int strSortId = 0; String strSort = ""; for (int I = 0; I
Params = new HashMap
(); Try {params = managerService. getUserList (parameter);} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();} String count = (String) params. get ("COUNT"); // total String [] [] strData = (String [] []) params. get ("AO_DATA"); // return successed (new DataTableReturnObject (Integer. parseInt (count), Integer. parseInt (count), sEcho, strData ));}
4. Query Method
Public Map
GetUserList (Map
QueryParams) throws Exception {String iCount = Total number of records; // converts the query result to a two-dimensional array String [] [] data ={}; if (lstUser! = Null & lstUser. size ()> 0) {int record = lstUser. size (); data = new String [record] []; for (int I = 0; I <lstUser. size (); I ++) {User objUser = (User) lstUser. get (I); JSONUser jsonUser = new JSONUser (); BeanUtils. copyProperties (jsonUser, objUser); data [I] = jsonUser. toArray () ;}} queryParams. clear (); // map, reset the value to use queryParams. put ("AO_DATA", data); queryParams. put ("COUNT", iCount); return queryParams ;}
Note that the attributes of the stored array object must be the same as the number of columns displayed on the front-end page.