I have a dataTable initialized with server side paging and it is working fine. This table triggers ajax, pulls data and renders onto the table during initialization. However I need empty table initially and load table data on click of a button using load() or reload() like:
myTable.api().ajax.reload();
Here is my table initialization:
function initTestTable(){
myTable = $('#testTable').dataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "testTableData.html",
"type": "GET",
},
"columns": [
{ "data": "code" },
{ "data": "description" }
]
});
}
There should be a way to restrict the loading of table during initialization? I read the documentation but could not find. Please suggest.
You could use the deferLoading parameter and set it to
0
. This will delay the loading of data until a filter, sorting action or draw/reload Ajax happens programmatically.To trigger the Ajax when the button is clicked you can have something like the following in the handler:
See example below for demonstration.
This is how I initially load my empty dataTable on page load. Then I load it with data via ajax using an eventListener. I couldn't find any documentation I just kind of played around with it and it works like a charm.
ref files - dataTables.js, table-advanced.js
NOTE: You could add some logic that selects a default option if there is one that is available and not disabled.
I could do it with a workaround by passing an extra parameter with the URL to identify the event.
For example, for on load I initialized the data table with
action="load"
as query param and for other action like search, am passingaction="search"
. With this I, at the back end, will be able to identify the call origin. If it is anything other than"load"
, I am pulling the data & passing (as the implementation is now). Otherwise (if "load") then I am passing empty data, which will show me"No Data Found"
message as if it did not made the ajax call.Here is my code - Table initialization:
For events other than load (say button click):
This was the one I had to take up without modifications to table init that would cause errors.
Thank you @JSelser and @davidkonrad for all your suggestions :)