jquery datatable後臺封裝資料示例程式碼

NO IMAGE

1.資料轉換類


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幫助類


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

3.實現類


public JSONResponse searchList(HttpServletRequest request , HttpServletResponse response ,String sEcho) throws Exception { 
//convertToMap定義於父類,將引數陣列中的所有元素加入一個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  ) //從傳遞引數裡面選出待用的引數 
{ 
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) ;//排序列數 
} 
if(strName.startsWith("sSortDir_")){ 
strSort = strValue;//排序的方向 "desc" 或者 "asc". 
} 

} 

Map<Object, Object> params = new HashMap<Object, Object>() ; 
try { 
params = managerService.getUserList(引數); 
} catch (Exception e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
String count = (String)params.get("COUNT");//總數 
String[][] strData = (String[][])params.get("AO_DATA");//當前頁顯示的集合 
return successed(new DataTableReturnObject(Integer.parseInt(count) , Integer.parseInt(count), sEcho, strData)); 
}

4.查詢方法


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

String iCount = 總記錄數; 
// 將查詢結果轉換為一個二維陣列 
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,重新設值使用 
queryParams.put("AO_DATA", data); 
queryParams.put("COUNT", iCount); 
return queryParams; 
}

注意存放的陣列物件的屬性必須與前端頁面顯示的列保持一樣的個數

您可能感興趣的文章:

解決3.01版的jquery.form.js中文亂碼問題的解決方法JQuery中dataGrid設定行的高度示例程式碼Jquery下EasyUI元件中的DataGrid結果集清空方法jQuery中使用data()方法讀取HTML5自定義屬性data-*例項對 jQuery 中 data 方法的誤解分析jquery用data方法獲取某個元素上的事件bootstrap data與jquery .datajquery操作HTML5 的data-*的用法例項分享jQuery表格外掛datatables用法總結jQuery 3.0 的變化及使用方法淺析jQuery 3.0中的Data