gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章jquery datatable后台封装数据示例代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

1.数据转换类 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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帮助类 。

?
1
2
3
4
5
6
7
8
public class BaseController {
protected JSONResponse successed(Object obj) {
JSONResponse ret = new JSONResponse();
ret.setSuccessed( true );
ret.setReturnObject(obj);
return ret;
}
}

3.实现类 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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.查询方法 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 datatable后台封装数据示例代码的文章就讲到这里了,如果你想了解更多关于jquery datatable后台封装数据示例代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com