欢迎来到代码驿站!

jquery

当前位置:首页 > 网页前端 > jquery

jquery datatable后台封装数据示例代码

时间:2020-12-20 11:51:55|栏目: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; 
}

注意存放的数组对象的属性必须与前端页面显示的列保持一样的个数

上一篇:jQuery实现的表格展开伸缩效果实例

栏    目:jquery

下一篇:基于jquery的一行代码轻松实现拖动效果

本文标题:jquery datatable后台封装数据示例代码

本文地址:http://www.codeinn.net/misctech/34564.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有