如何使用dataTables实例化不加载数据的表(服务器模式),然后加载数据当我点击一个button.If时serverSide设置为true在初始化时,表会自动发送一个ajax请求,然后呈现数据,这不是我想要的!:(
发布于 2016-12-09 21:37:25
在看了半天的源代码后,我终于找到了解决方案。首先,我需要一个名为firstAjax的自定义参数,并将其设置为false。如下所示:
$("#example").DataTable({
serverSide: true,
ajax: {
url: 'your url'
},
firstAjax: false
});
然后我更改了
_fnReDraw (settings); //in datatables.js line 4717
至
if (settings.oInit.firstAjax) {
_ fnReDraw (settings);
}
如果是压缩的js文件(datatables.min.js),应该会找到_fnReDraw函数对应的别名。
发布于 2017-10-25 19:24:37
初始化DataTables时,应该在参数中使用"iDeferLoading“:0:
var table = $("#table").dataTable({
"bProcessing": true,
"bServerSide": true,
"iDeferLoading": 0,
"sAjaxSource": service_url,
"sServerMethod": "POST",
...
...
(或者“deferLoading”:对于较新的DataTables版本,为0,1.10及更高版本),然后将该事件添加到您的按钮:
$("#button").on("click", function (event) {
$('#table').dataTable().fnDraw();
});
https://datatables.net/examples/server_side/defer_loading.html
发布于 2016-12-09 15:09:30
在类似的情况下,我就是这样做的。
<script>
$(function ($) {
$("#tbl").DataTable({columns:[{data:"id"}, {data:"text"}], dom: "tB", buttons: [{ text: "Load Me", action: function (e, dt, node, config) { loadme(e, dt, node, config); } }] });
}
);
// // parameters are passed from the datatable button event handler
function loadme(e, dt, node, config) {
parms = JSON.stringify({ parm1: "one", parm2: "two" });
$.ajax({
// my test web server that returns an array of {id:"code field", text:"country namee"}
url: "WebService1.asmx/GetAList",
data: JSON.stringify({ s: parms }),
type: 'post',
contentType: "application/json; charset=utf-8",
dataType: "json",
// this is just away of passing arguments to the success handler
context:{dt:dt, node:node},
success: function (data, status) {
var contries = JSON.parse(data.d);
for (var i = 0; i < contries.length; i++){
this.dt.row.add(contries[i], "id", "text");
this.dt.draw();
}
},
error: function (one, two) {
debugger;
}
});
}
</script>
</head>
<body>
<div style="width:500px">
<table id="tbl">
<thead>
<tr>
<th>Code</th>
<th>Contru</th>
</tr>
</thead>
<tbody></tbody>
<tfoot></tfoot>
</table>
</div>
</body>
https://stackoverflow.com/questions/41054119
复制相似问题