问题
I've build simple table using DataTables and I successfully connected server-side script to populate my table with data.
Above my table I have 2 checkboxes and 2 date selects to specify filter criterias. When my page is loading I get table filled, because at startup datatable is pulling first page of data from server.
I would like to disable first initial data load, so when page is loaded I'll get empty table, then after I'll select criterias and press 'Load' button data will be loaded.
I know how to add my filter criterias to server params, what I need to do is to load data from server after user click button.
Below is my datatable script:
var myTable= $('table#myTable').dataTable({
"table-layout": "fixed",
"bJQueryUI": true,
"sDom": '<"H"lpr>t<"F"ip>',
"iDisplayLength": 25,
"aLengthMenu": [[25, 50, 100, 500], [25, 50, 100, 500]],
"bSort": false,
"sPaginationType": "full_numbers",
"bPaginate": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "Data.asmx/Sales",
"fnServerData": function(sSource, aoData, fnCallback) {
var sEcho = aoData[0].value;
var iDisplayStart = aoData[3].value;
var iDisplayLength = aoData[4].value;
$.ajax({
contentType: "application/json; charset=utf-8",
type: "POST",
url: sSource,
//below are my parameters
data: "{'sEcho': '" + sEcho
+ "','iDisplayStart': '" + iDisplayStart
+ "','iDisplayLength': '" + iDisplayLength
+ "'}",
success: function(msg) {
fnCallback(msg.d);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.responseText);
}
});
},
"bAutoWidth": false,
"aoColumns": [{
"sType": "numeric",
"mData": "Nr",
"sWidth": "50px"
}, {
"sType": "string",
"mData": "Name"
}, {
"sType": "string",
"mData": "Surname"
}]
});
回答1:
You simply specify the iDeferLoading parameter to datatables, with the number of records your table contains already in the DOM, or 0 if no records.
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "scripts/server_processing.php",
"iDeferLoading": 0
} );
} );
回答2:
Don't call the .dataTable()
method until you want the table. Example below:
var myTable = null,
dtOptions = {
"table-layout": "fixed",
"bJQueryUI": true,
//...rest of options...
};
$('#button').click(function (e) {
myTable = $('table#myTable').dataTable(dtOptions);
});
or, if you don't need to access options after the table has been initialized:
$('#button').click(function (e) {
myTable = $('table#myTable').dataTable({
"table-layout": "fixed",
"bJQueryUI": true,
"sDom": '<"H"lpr>t<"F"ip>',
"iDisplayLength": 25,
"aLengthMenu": [[25, 50, 100, 500], [25, 50, 100, 500]],
"bSort": false,
"sPaginationType": "full_numbers",
"bPaginate": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "Data.asmx/Sales",
"fnServerData": function(sSource, aoData, fnCallback) {
var sEcho = aoData[0].value;
var iDisplayStart = aoData[3].value;
var iDisplayLength = aoData[4].value;
$.ajax({
contentType: "application/json; charset=utf-8",
type: "POST",
url: sSource,
//below are my parameters
data: "{'sEcho': '" + sEcho
+ "','iDisplayStart': '" + iDisplayStart
+ "','iDisplayLength': '" + iDisplayLength
+ "'}",
success: function(msg) {
fnCallback(msg.d);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.responseText);
}
});
},
"bAutoWidth": false,
"aoColumns": [{
"sType": "numeric",
"mData": "Nr",
"sWidth": "50px"
}, {
"sType": "string",
"mData": "Name"
}, {
"sType": "string",
"mData": "Surname"
}]
});
});
来源:https://stackoverflow.com/questions/16086090/datatables-and-server-side-processing-load-on-demand