I am using jQuery DataTables version 1.10.12. Below is my relevant code
我使用的是jQuery DataTables 1.10.12版本。以下是我的相关代码
// initialize datatable
jQuery('#taskTable').DataTable( {
"bProcessing": true,
"bServerSide": true,
"bInfo": true,
"bPaginate": true,
"bLengthChange":false,
"pageLength": 10,
"sAjaxDataProp":"serverPageDataModelBean.data"
});
Data is being displayed and pagination is working. However when I set "bInfo": true
I get the following message:
数据正在显示,分页正在工作。但是,当我设置“bInfo”:true时,我会收到以下消息:
Showing 0 to 0 of 0 entries (filtered from NaN total entries)
and my Next button does not work. In serverPageDataModelBean I am sending {"recordsTotal":12,"recordsFiltered":12,"data":[]}
where data is not empty.
我的“下一步”按钮不起作用。在serverPageDataModelBean中,我正在发送{“recordsTotal”:12、“recordsFiltered”:12和“data”:[]},其中数据不为空。
Can someone please tell me what is wrong?
有人能告诉我怎么了吗?
更多回答
I know this too late, but I'm posting this in case someone stumbles on this.
The answer is you need to set serverside to false like this "serverSide": false.
我知道这太晚了,但我会把它发布出来,以防有人无意中发现。答案是您需要将serverside设置为false,如“serverside”:false。
CAUSE
Your server-side response is missing draw
parameter which should have the same value as the draw
parameter in the request. When that happens, jQuery DataTables discards the data.
服务器端响应缺少draw参数,该参数的值应与请求中的draw参数相同。当这种情况发生时,jQueryDataTables会丢弃数据。
draw
The draw counter that this object is a response to - from the draw
parameter sent as part of the data request.
SOLUTION
Return draw
parameter with the same value as draw
parameter from the request.
返回与请求中的绘制参数具有相同值的绘制参数。
LINKS
See Server-side processing - Returned data for more information.
有关详细信息,请参阅服务器端处理-返回的数据。
If you are using serverside: true
option, then remove this or write serverside: false
如果使用serverside:true选项,则删除此选项或写入serverside:false
for example:
例如:
$(document).ready(function() {
$('#example1').dataTable( {
"ajax": {
"url": "view_results",
"type": "POST"
},
'order':[]
} );
});
the response for empty data when handling on server side for example php will be ...
在服务器端处理(例如php)时,对空数据的响应将是。。。
echo json_encode(
array(
'draw' => $_POST['draw'],
'data' => [],
'recordsFiltered' => 0,
'recordsTotal' => 0
)
);
In pure json
纯json
{"draw":1,"data":[],"recordsFiltered":0,"recordsTotal":0}
In short no matter what Datatable as on 2020 needs this parameters on server side handling.
简而言之,无论2020年的数据表是什么,服务器端处理都需要这些参数。
removing the serverSide attribute(or making it false) is not the right solution. This would break if you have pagination set to true and the sitewide pagination is controlling your data being displayed. If you dont have any such customisation then off-the-shelf pagination by jQuery will do the job.
删除serverSide属性(或使其为false)不是正确的解决方案。如果您将分页设置为true,并且站点范围内的分页控制着显示的数据,那么这种情况就会中断。如果你没有任何这样的定制,那么jQuery的现成分页就可以了。
The solution taht shall work all time is setting the 'draw' attribute in the json returned by your servercall.
一直有效的解决方案是在服务器调用返回的json中设置“draw”属性。
I can provide an example from Coldfusion perspective. the idea is the same. ensure the json you return back to the server is having the 'draw' parameter added.
我可以从Coldfusion的角度提供一个例子。想法是一样的。确保返回到服务器的json中添加了“draw”参数。
<cfquery name="qryItemList" datasource="#session.dbsource#">
SELECT item 1,
item 2,
COUNT(*) OVER () as TotalCount
FROM Items
</cfquery>
<cfif len(qryItemList.TotalCount) NEQ 0>
<cfset Total= qryItemList.TotalCount>
<cfelse>
<cfset Total= 0>
</cfif>
<cfset dataset = queryToStruct(qryItemList)> //queryToStruct is a UDF to convert an entire query to a struct
<cfset dataset = serializejson(dataset)>
<cfset dataset = ReplaceNoCase(dataset,'"recordcount":#qryItemList.recordcount#','"recordsTotal":#Total#,"recordsFiltered":#Total#, "draw":"0"')>
I have tried serverside: false
but it didn't work, also make sure to check that the variables in ajax use proper camelCase, i.e. a captital T
in recordsTotal and a capital F
in recordsFiltered. It should be like below:
我尝试过serverside:false,但它不起作用,还确保检查ajax中的变量是否使用了正确的camelCase,即recordsTotal中的标题t和recordsFiltered中的大写F。它应该如下所示:
$output = array(
'data'=>$data,
'draw'=>$_POST['draw'],
'recordsTotal'=>$filtered_rows,
'recordsFiltered'=>$countrow,
);
echo json_encode($output);
Make "serverSide": false or remove the line.
but make sure that making "serverSide": false will be okay for short data tables
For larger ones, it won't support or not suggested
将“serverSide”设为false或删除该行。但请确保将“serverSide”:false设置为适用于短数据表。对于较大的数据表,它不支持或不建议使用
更多回答
Thank You !! Really appreciate it.
非常感谢。真的很感激。
@Mamadou Barry what if I am fetting the data from the ajax call from backend, If I make serverside false or remove then no data shows
@Mamadou Barry,如果我从后端处理ajax调用的数据,如果我使服务器端为false或删除,那么没有数据显示
I know this is too late. But how can we do that?
我知道这已经太晚了。但我们怎么能做到呢?
@Saitama A simple JSON response data the client's AJAX call will receive, containing the parameters of type Number
: draw
, recordsFiltered
, and recordsTotal
, should suffice.
@Saitama客户端的AJAX调用将收到一个简单的JSON响应数据,其中包含类型为Number的参数:draw、recordsFiltered和recordsTotal,就足够了。
我是一名优秀的程序员,十分优秀!