自定义数据源属性dataSrc和分页问题 [英] Custom data source property dataSrc and pagination issue
问题描述
所以问题是我从服务器获取响应如JSON:
As您可以在此JSON响应中看到,所需的数据表需要JSON位于 data.data
中,以将数据源设置为数据类型,该属性为
请注意,我无法从服务器端更改JSON响应。
更新:
这里是js call script:
$(document).ready(function(){
$(#example .dataTable({
ajax:{
url:app.getApiUrlWithAccessToken('lead / get_all'),
dataSrc:function(json){
return json.data。数据;
}
},
lengthMenu:[1,2,5,10,15],
列:[
{data :first_name},
{data:last_name},
{data:title},
{data:email},
{data:city},
{data: status}
],
处理:true,
serverSide:true
});
});
原因
在服务器端处理模式中,DataTables期望返回的某些结构数据。参数 draw
, recordsTotal
和 recordsFiltered
应该是顶级属性。您的回复具有这些参数作为数据
的子属性,而不是DataTables将在寻找他们。
解决方案
设置参数 draw
, recordsTotal
和 recordsFiltered
作为DataTable期望的JSON响应的顶级属性。
使用以下代码 ajax.dataSrc
选项:
dataSrc:function(json){
json.draw = json.data.draw;
json.recordsTotal = json.data.recordsTotal;
json.recordsFiltered = json.data.recordsFiltered;
return json.data.data;
}
演示
请参阅此jsFiddle 进行代码和演示。
I'm working with jQuery DataTables and server-side processing mode. But I'm facing an issue with data table, I've search every thing in Datatables documentation but couldn't find my answer.
So the problem is I'm getting response from server as JSON like this:
As you can see in this JSON response, datatables required JSON is in data.data
to set this data source in datatables there is a property which is Custom Data Property and it working fine and shows the rows. Now problem is that datatables is not considering pagination parameters from JSON which is why it show's this:
Please note that I cannot change JSON response from server side.
Update: Here is js call script:
$(document).ready(function () {
$("#example").dataTable({
"ajax": {
url: app.getApiUrlWithAccessToken('lead/get_all'),
dataSrc: function(json){
return json.data.data;
}
},
"lengthMenu": [1,2,5,10,15],
"columns": [
{ "data": "first_name" },
{ "data": "last_name" },
{ "data": "title" },
{ "data": "email" },
{ "data": "city" },
{ "data": "status" }
],
"processing": true,
"serverSide": true
});
});
CAUSE
In server-side processing mode DataTables expects certain structure in returned data. Parameters draw
, recordsTotal
and recordsFiltered
should be top-level properties. You response has these parameters as sub-properties of data
, not where DataTables would be looking for them.
SOLUTION
Set parameters draw
, recordsTotal
and recordsFiltered
as top-level properties of JSON response where DataTables expects them to be.
Use the following code for ajax.dataSrc
option:
dataSrc: function(json){
json.draw = json.data.draw;
json.recordsTotal = json.data.recordsTotal;
json.recordsFiltered = json.data.recordsFiltered;
return json.data.data;
}
DEMO
See this jsFiddle for code and demonstration.
这篇关于自定义数据源属性dataSrc和分页问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!