'DataTables auto refresh function doesn't work

I have an issue with the reload function for DataTables.

I am using the following code to load and reload the table (server-side).

$( document ).ready(function() {
    $('#dienst_tabelle').DataTable( {
        "ajax": "getData.php",
        "serverSide": true,
        "bProcessing": true,
        "bPaginate":false,
        "searching": false,
        "paging": false, 
        "info": false,
        "columns": [
            { mData: 'dienstname' } ,
            { mData: 'tagesart' },
            { mData: 'gueltig_am' },
            { mData: 'dienstbeginn' },
            { mData: 'dienstende' },
            { mData: 'zeit' },
            { mData: 'status' },
            { mData: 'button' }
        ]
    });

    setTimeout(function(){
        $('#dienst_tabelle').DataTable().ajax.reload(null, false);
    }, 3000);
});

The table loads successfully, but the reload-function doesn't do anything...

What am I doing wrong?



Solution 1:[1]

I can recreate your problem, if I don't handle the server-side draw parameter correctly.

The documentation for this parameter states:

The draw counter that this object is a response to - from the draw parameter sent as part of the data request.

Every time DataTables sends a request to the server it will increment this integer counter (starting at 1). It is the server's job to ensure that the draw parameter it sends back in its response corresponds to the parameter it received in the request.

So, for example, if you hard-code the draw parameter on the server to 1, then the request generated by setTimeout will have a draw value of 2 and the server will respond with a draw value of 1. This mismatch will result in the response being received, but ignored by DataTables.

The solution is for your server-side code to retrieve the draw value from the request and return that same exact value (as an integer) in the response. You are using PHP, which I do not use - so either you already know how to access request parameters using PHP, or you may need to research that.

(In your case, where you are using GET, then maybe you need to use $_GET or $_REQUEST in your PHP.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 andrewJames