我的数据表(jquery datatables 1.10.5)服务器端处理为true。除了分页之外,一切都在这里工作。这是jsfiddle http://jsfiddle.net/e9btzpox/5/ 。我搜索过这么多帖子,到处都是" recordsTotal"和" recordsFiltered"应该是平等的,但这也不起作用。
以下是我的代码示例:
$table.dataTable({
"serverSide": true,
"processing": true,
"processing": true,
"columns": [
["data", 1],
["data", 2]
],
"ajax": {
"url": "/test/0",
"dataSrc": function (json) {
var data = json.aaData;
return data;
}
},
"createdRow": function (row, data, index) {
var chk_name = 'chk_' + data[0].id;
$('td:eq(0)', row)
.html('<input name="' + chk_name + '" type="checkbox" value="1">');
var select_name = 'select_' + data[0].id;
html =
'<select name="' + select_name + '">' + '<option value="">Select one</option>' + '<option>' + data[2].operation[0] + '</option>' + '<option>' + data[2].operation[1] + '</option>'; + '</select>';
$('td:eq(1)', row).html(html);
},
});
以下是json响应的示例: -
{"draw": 1,
"recordsTotal": "11",
"recordsFiltered": "11",
"aaData": [
[{
"id": 1
}, {
"chk": "on"
}, {
"operation": ["Modify", "Delete"]
}]
}
使用服务器端代码进行更新: -
public class DataTableObject {
int draw;
String recordsTotal;
String recordsFiltered;
List<Student> data;
public int getDraw() {
return draw;
}
public void setDraw(int draw) {
this.draw = draw;
}
public String getRecordsTotal() {
return recordsTotal;
}
public void setRecordsTotal(String recordsTotal) {
this.recordsTotal = recordsTotal;
}
public String getRecordsFiltered() {
return recordsFiltered;
}
public void setRecordsFiltered(String recordsFiltered) {
this.recordsFiltered = recordsFiltered;
}
public List<Student> getData() {
return data;
}
public void setData(List<Student> data) {
this.data = data;
}
public List<Student> getAaData() {
return data;
}
public void setAaData(List<Student> aaData) {
this.data = aaData;
}
}
和json的回应正在准备中:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
List<Students> listOfStudent= getStudentList();
DataTableObject dataTableObject = new DataTableObject();
dataTableObject.setDraw((Integer.valueOf(request.getParameter("draw"))));
dataTableObject.setRecordsTotal("2000");
dataTableObject.setRecordsFiltered("2000");
dataTableObject.setAaData(listOfStudent);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(dataTableObject);
out.print(json);
}
public List<Students> getListOfStudents(){
List<Students> listOfStudent = new ArrayList<Students>();
for (int i = 1; i <= 200; i++) {
aStudent = new Student();
String [] names= new String [3];
names[0] = "Sandeep"+i;
names[1] = "Jaspal"+i;
names[2] = "Harpal"+i;
aStudent.setId(""+i);
aStudent.setChk("off");
aStudent.setOperation(new String[]{"modify","delete"});
listOfStudent.add(aStudent);
}
return listOfStudents;
}
答案 0 :(得分:1)
使用1.10,服务器获取名为start
&amp;的参数。 length
用于分页。您需要使用这些内容使用Skip()
和Take()
来分页学生列表。
e.g。
var start = Integer.valueOf(request.getParameter("start"));
var length = Integer.valueOf(request.getParameter("length"));
List<Students> listOfStudent = getStudentList();
List<Students> filteredList = listOfStudent.Skip(start).Take(length)
dataTableObject.setAaData(filteredList);
这也意味着您的记录数量(全部和已过滤)应该是这些列表中的项目数:
e.g。类似的东西:
dataTableObject.setRecordsTotal(listOfStudent.Count());
dataTableObject.setRecordsFiltered(filteredList.Count());
注意:您没有显示对搜索和排序的任何支持,但它们需要在分页之前发生。
答案 1 :(得分:1)
当我将recordsTotal和recordsFilteres值发送为字符串而不是整数时,我在分页部分看到了一些问题。因此,您的值应如下所示,没有引号:
"recordsTotal": 11,
"recordsFiltered": 11,
答案 2 :(得分:0)
让我解释一下,假设我们使用jquery datatable服务器端分页,start = 0,length = 25
recordsFiltered:(此处年龄是过滤的动态参数)
select * from students where IsDeleted = 0 AND age = 18 limit start, length
或者可以是
select * from students where IsDeleted = 0 limit start, length
recordsTotal:(如果您使用,这是错误的)
select count(1) as Total from students where IsDeleted = 0 limit start, length
recordsTotal:(这是完全使用与recordsFiltered相同的查询但省略限制子句的正确方法)
select count(1) as Total from students where IsDeleted = 0 AND age = 18
或者可以是
select count(1) as Total from students where IsDeleted = 0 limit start, length