My datatable doesn't refresh after AJAX Call

My datatable doesn't refresh after AJAX Call

saiabhiramsaiabhiram Posts: 4Questions: 1Answers: 0

Hello I have set up my data table. I am able to show data on page load. However when I click on next page, the call is made to server side and it returns data, however the data table doesn't refresh. Can anyone tell me what is going on ?

$(document).ready(function () {
//get Data Tables Query
buildDataTables();
});

function buildDataTables() {
if (!$.fn.DataTable.isDataTable('#locationTable')) {
$('#locationTable').DataTable({
"bServerSide": true,
"sAjaxSource": '/Home/GetDataTablesQuery',
paging: true,
"iDisplayLength": 10,
"aoColumns": [
{ "mData": "Address1", "name": "Address 1" },
{ "mData": "City", "name": "City" },
{ "mData": "Size", "name": "Approx SF" },
{ "mData": "StoreNumber", "name": "Store #" },
]
});
}
How do I refresh datatable data on pagination ??

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    The place to start is to see what data is returned for each page. This tech note will describe how to look at the ajax response for the page request. My guess is the same data is being returned for each page. Datatables will automatically update the display showing the data returned.

    Kevin

  • saiabhiramsaiabhiram Posts: 4Questions: 1Answers: 0

    Kevin,

    I am able to show data on page load. On clicking on page 2 or another page the data doesn't refresh. I don't see any error on the console either.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    are you using legacy datatables? The terms you are using are not consistent with the latest version of DataTables

  • shrutig8shrutig8 Posts: 3Questions: 1Answers: 0

    you can do $('#dataTable).DataTable().draw(); after your ajax call.

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    On clicking on page 2 or another page the data doesn't refresh. I don't see any error on the console either.

    That's why I suggested using using the steps in the tech note to look at the data returned from the server as a place to start. Is the data returned different when you go to page 2?

    Kevin

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin
    Answer ✓

    At a guess the old sEcho value isn't changing. You need to have it reflect the value that was sent to the server.

    Allan

  • saiabhiramsaiabhiram Posts: 4Questions: 1Answers: 0

    Allan - Bingo !!! That was the issue. I was always setting sEcho to 10

  • saiabhiramsaiabhiram Posts: 4Questions: 1Answers: 0

    Everyone else thanks for your suggestions !!!

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    Make sure you cast it as an integer on the server-side to you don't have any possible security issues. If you just echo back what the client sends it can be open to attack.

    Allan

This discussion has been closed.