jquery datatable后臺封裝數(shù)據(jù)示例代碼
更新時間:2014年08月07日 15:22:57 投稿:whsnow
這篇文章主要介紹了jquery datatable后臺封裝數(shù)據(jù)的示例代碼,需要的朋友可以參考下
1.數(shù)據(jù)轉(zhuǎn)換類
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.實現(xiàn)類
public JSONResponse searchList(HttpServletRequest request , HttpServletResponse response ,String sEcho) throws Exception { //convertToMap定義于父類,將參數(shù)數(shù)組中的所有元素加入一個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++) //從傳遞參數(shù)里面選出待用的參數(shù) { 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) ;//排序列數(shù) } if(strName.startsWith("sSortDir_")){ strSort = strValue;//排序的方向 "desc" 或者 "asc". } } Map<Object, Object> params = new HashMap<Object, Object>() ; try { params = managerService.getUserList(參數(shù)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } String count = (String)params.get("COUNT");//總數(shù) String[][] strData = (String[][])params.get("AO_DATA");//當(dāng)前頁顯示的集合 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 = 總記錄數(shù); // 將查詢結(jié)果轉(zhuǎn)換為一個二維數(shù)組 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,重新設(shè)值使用 queryParams.put("AO_DATA", data); queryParams.put("COUNT", iCount); return queryParams; }
注意存放的數(shù)組對象的屬性必須與前端頁面顯示的列保持一樣的個數(shù)
您可能感興趣的文章:
- 解決3.01版的jquery.form.js中文亂碼問題的解決方法
- JQuery中dataGrid設(shè)置行的高度示例代碼
- Jquery下EasyUI組件中的DataGrid結(jié)果集清空方法
- jQuery中使用data()方法讀取HTML5自定義屬性data-*實例
- 對 jQuery 中 data 方法的誤解分析
- jquery用data方法獲取某個元素上的事件
- bootstrap data與jquery .data
- jquery操作HTML5 的data-*的用法實例分享
- jQuery表格插件datatables用法總結(jié)
- jQuery 3.0 的變化及使用方法
- 淺析jQuery 3.0中的Data
相關(guān)文章
jQuery實現(xiàn)移動端手機(jī)商城購物車功能
這篇文章主要介紹了jQuery實現(xiàn)移動端手機(jī)商城購物車功能的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09Bootstrap table中toolbar新增條件查詢及refresh參數(shù)使用方法
這篇文章主要介紹了Bootstrap table中toolbar新增條件查詢及refresh參數(shù)使用方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-05-05jQuery實時顯示鼠標(biāo)指針位置和鍵盤ASCII碼
本文通過jquery技術(shù)實現(xiàn)鼠標(biāo)指針位置和鍵盤ASCII碼,非常具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-03-03