DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jquery datatable後台封裝數據示例代碼
jquery datatable後台封裝數據示例代碼
編輯:JQuery特效代碼     

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; 
}

注意存放的數組對象的屬性必須與前端頁面顯示的列保持一樣的個數

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved