Jquery datatable backend wraps the data sample code

  • 2020-03-30 03:38:46
  • OfStack

1. Data conversion class


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 to help class


public class BaseController { 
protected JSONResponse successed(Object obj) { 
JSONResponse ret = new JSONResponse(); 
ret.setSuccessed(true); 
ret.setReturnObject(obj); 
return ret; 
} 
}

3. The implementation class


public JSONResponse searchList(HttpServletRequest request , HttpServletResponse response ,String sEcho) throws Exception { 
//ConvertToMap, defined in the parent class, adds all the elements in the parameter array to a HashMap
Map<Object, Object> objQueryMap = new HashMap<Object, Object>(); 
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<jsonarray.size();i++) //Select the parameter to be used from the pass parameter
{ 
JSONObject obj=(JSONObject)jsonarray.get(i); 
String strName = (String)obj.get("name"); 
String strValue = obj.get("value").toString(); 
if(strName.equals("sEcho")){ 
sEcho=strValue; 
} 
if(strName.equals("iDisplayStart")) { 
strDisplayStart=strValue; 
} 
if(strName.equals("iDisplayLength")) { 
strDisplayLength=strValue; 
} 
if(strName.equals("sColumns")){ 
arrayColumen = obj.get("value").toString().split(","); 

} 
if(strName.startsWith("iSortCol_")){ 
strSortId = Integer.parseInt(strValue) ;//Row of the sequence number
} 
if(strName.startsWith("sSortDir_")){ 
strSort = strValue;//Sort in the direction of "desc" or "asc".
} 

} 

Map<Object, Object> params = new HashMap<Object, Object>() ; 
try { 
params = managerService.getUserList( parameter ); 
} catch (Exception e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
String count = (String)params.get("COUNT");// The total number of  
String[][] strData = (String[][])params.get("AO_DATA");//The collection displayed on the current page
return successed(new DataTableReturnObject(Integer.parseInt(count) , Integer.parseInt(count), sEcho, strData)); 
}

4. Query method


public Map<Object, Object> getUserList(Map<Object, Object> queryParams) 
throws Exception { 

String iCount =  The total number of records ; 
//Converts the query results 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();//Case map, reset the value to use
queryParams.put("AO_DATA", data); 
queryParams.put("COUNT", iCount); 
return queryParams; 
}

Note that the array objects must have the same number of properties as the columns displayed on the front page


Related articles: