jquery - 为什么分页不能在jquery数据表的服务器端proccessing上工作,即使"recordsTotal" 和"recordsFiltered" 相同

这是jsfiddle http://jsfiddle.net/e9btzpox/5/

下面是代码示例:


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

时间:

对于1.10,服务器获取称为start & length的参数,你需要使用这些函数来访问使用Skip()Take()的学生列表。

比如,


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

这也意味着你的记录数(全部和筛选)应该是这些列表中的项的计数:

例如,类似于:


dataTableObject.setRecordsTotal(listOfStudent.Count());
dataTableObject.setRecordsFiltered(filteredList.Count());

注意:你还没有显示搜索和排序支持,但是它们需要在分页之前出现。

...