我在serverside = true中使用DataTables。只有当服务器上的数据被更改时,我才想重绘我的数据表。但是table.ajax.reload()在每次调用时都会重新绘制表。
你能举个小例子吗?
民主联盟根据评论意见:
我有简单的服务器端处理datatable。如果用户更改页面、排序或筛选器或其他任何内容,则ajax查询将发送到服务器以接收新数据和重绘表。但是服务器上的数据可能会改变。其他用户向数据库表插入新行或更改某些内容,例如。我想如果这发生数据数据自动刷新。但这是罕见的事件。不需要重新绘制所有数据每10 (或一些)秒。只有当服务器上的某些内容发生更改时,才需要重新绘制。对服务器的查询是快速的。重新绘制很慢。
发布于 2020-12-03 19:58:56
这是一个很好的问题--我的DataTables中有按钮、输入等等。当重绘发生时,可能会出现像textbox或错误单击按钮这样的问题。为了避免这种情况,我将DataTables设置为只有在有来自服务器的新的/更新的数据时才重新绘制。
下面是操作步骤:
template.html
<!-- include lodash _ used to determine json equality -->
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>script.js
// global variables that are updated with each new ajax reload call:
var last_response = {};
var redraw = false;
// DataTable init:
var table = $('<selector>').DataTable({
// initial settings:
"columns" : ["<the columns>"],
...
// override the dataSrc parameter to handle data upon receiving a new response:
"ajax" : {
"url" : "<the url>",
"dataSrc": function(response) {
// compare response to the last response using lodash:
if (_.isEqual(last_response, response) redraw = false;
else redraw = true;
// set the last_response global variable:
last_response = response;
// return appropriately formatted data:
return data;
}
// use the predraw callback to determine whether to draw or not:
"preDrawCallback" : function() {
// return true to draw or false to skip draw:
return redraw;
}
});https://stackoverflow.com/questions/48707696
复制相似问题