DataTables ajax.reload() not working
DataTables ajax.reload() not working
I want to refresh(Ajax Call) my datatable in every 30 second without paging reset. Please help me
My Table:
var handleDataTableButtons = $("#datatable-buttons").dataTable({
"destroy": true,
"order": [],
dom: "Bfrtip",
"bProcessing": false,
"bServerSide": false,
"searching": false,
"pageLength": 10,
"autoWidth": false,
"bAutoWidth": false, // Disable the auto width calculation
"sort": "position",
"stateSave": true,
"ajax": {
processing: true,
serverSide: true,
url: "myurl",
type: "POST",
crossDomain: false,
data: {},
dataType: "json",
success: function (result) {
handleDataTableButtons.fnClearTable();
handleDataTableButtons.fnAddData(MyDataList);
handleDataTableButtons.fnDraw();
}
},
"columnDefs": [],
"language": {
"lengthMenu": "Display _MENU_ records per page",
"zeroRecords": "No records available",
"infoEmpty": "",
"loadingRecords": "Loading...",
"processing": "Processing..."
},
"rowId": 'staffId',
"aoColumns": [
{"mData": "col1"},
{"mData": "col2"},
{"mData": "col3"},
{"mData": "col4"},
{"mData": "col5"},
{"mData": "col6"}
],
responsive: !0
});
This question has accepted answers - jump to:
Answers
Does the second example help:
https://datatables.net/reference/api/ajax.reload()#Examples
Kevin
I tried this, It's not working..
What is MyDataList? I don't see it defined anywhere.
Also please post a description of what;s not working. Are you getting errors?
Kevin
MyDataList is the json response coming after ajax call..
actually it is ---> result.MyDataList
A couple of issues in the code above. DataTables says not to override
success
in the ajax settings, seeajax
, it will prevent DataTables from processing the result (however, your code appears to be trying to add the rows itself instead of letting datatables do its thing).Also, the
processing
andserverSide
options belong to the table settings and not the ajax settings.Thank you for your help. I am very new In using DataTables.
I have changed my code but still facing same issue. Please help me
handleDataTableButtons = $("#datatable-buttons").dataTable({
"destroy": true,
"order": [],
dom: "Bfrtip",
"bProcessing": false,
"bServerSide": false,
"sort": "position",
"stateSave": true,
Please let me know how can I handle the Response coming from the server???
===============================================
Please find the my Json responce from the server. I want to Show MyList in the datatable
{
"sesssion": true,
"iTotalRecords": 2,
"MyList": [
{
"col1": "7008057810",
"col2": "20",
"col3": "In Progress",
"col4": "123456789",
"col5": "Australia",
"col6": "XXXXXXX"
},
{
"col1": "7008057810",
"col2": "20",
"col3": "In Progress",
"col4": "987654321",
"col5": "Australia",
"col6": "YYYYYY"
}
]
}
Try adding
ajax.dataSrc
to your ajax config. I think this should work:```
"ajax": {
url: "myUrl",
type: "POST",
dataSrc": "MyList"
},
Kevin
Thank you, Yes, Now on load of datatable I am getting data in table. but Unable to reload the Ajax in every 30 sec.
Please find my code below:
setInterval(function () {
I copied your function into this test case:
http://live.datatables.net/gafexuxu/1/edit
Seems to work. But 30 seconds seems like forever when sitting there watching it
Is the function not running or is there an issue with the ajax request or response?
Kevin
When I am calling same function it is not calling my ajax Url again (in every 30 sec),
I debug my code ajax request not going to my server.
My Code:
setInterval(function () {
}, 30000);
On console Only "Before Ajax Call" msg printing not After Ajax Call
What version of Datatables are you running?
To use
ajax.reload()
you need to be using 1.10.x. If you are using 1.10.x then you need to change this:to this:
notice the capital D in DataTable. This first FAQ here explains the difference:
https://datatables.net/faqs/index#Most-common-FAQs
Kevin
Yes It worked...Thank You Very Much