DataTables ajax.reload() not working

DataTables ajax.reload() not working

anemaanema Posts: 14Questions: 7Answers: 0

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

  • kthorngrenkthorngren Posts: 21,292Questions: 26Answers: 4,944
  • anemaanema Posts: 14Questions: 7Answers: 0

    I tried this, It's not working.. :(

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28

    What is MyDataList? I don't see it defined anywhere.

  • kthorngrenkthorngren Posts: 21,292Questions: 26Answers: 4,944

    Also please post a description of what;s not working. Are you getting errors?

    Kevin

  • anemaanema Posts: 14Questions: 7Answers: 0

    MyDataList is the json response coming after ajax call..
    actually it is ---> result.MyDataList

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28

    A couple of issues in the code above. DataTables says not to override success in the ajax settings, see ajax, 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 and serverSide options belong to the table settings and not the ajax settings.

  • anemaanema Posts: 14Questions: 7Answers: 0

    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,

        "ajax": {
            url: "myUrl",
            type: "POST",
        },
       "language": {
            "zeroRecords": "No records available",
            "loadingRecords": "Loading...",
            "processing": "Processing..."
        },
        "aoColumns": [
        {"mData": "col1"},
        {"mData": "col2"},
        {"mData": "col3"},
        {"mData": "col4"},
        {"mData": "col5"},
        {"mData": "col6"}
        ]
    });
    

    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"
    }
    ]
    }

  • kthorngrenkthorngren Posts: 21,292Questions: 26Answers: 4,944
    Answer ✓

    Try adding ajax.dataSrc to your ajax config. I think this should work:

    ```
    "ajax": {
    url: "myUrl",
    type: "POST",
    dataSrc": "MyList"
    },

    Kevin

  • anemaanema Posts: 14Questions: 7Answers: 0

    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 () {

            handleDataTableButtons.ajax.reload();
    
        }, 30000);
    
  • kthorngrenkthorngren Posts: 21,292Questions: 26Answers: 4,944

    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 :smile:

    Is the function not running or is there an issue with the ajax request or response?

    Kevin

  • anemaanema Posts: 14Questions: 7Answers: 0

    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 () {

    console.log('Before Ajax Call');
    
    handleDataTableButtons.ajax.reload();
    
    console.log('After Ajax Call');
    

    }, 30000);

    On console Only "Before Ajax Call" msg printing not After Ajax Call

  • kthorngrenkthorngren Posts: 21,292Questions: 26Answers: 4,944
    Answer ✓

    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:

    $("#datatable-buttons").dataTable({

    to this:

    $("#datatable-buttons").DataTable({

    notice the capital D in DataTable. This first FAQ here explains the difference:
    https://datatables.net/faqs/index#Most-common-FAQs

    Kevin

  • anemaanema Posts: 14Questions: 7Answers: 0

    Yes It worked...Thank You Very Much :) :)

This discussion has been closed.