How can i prevent DT from drawing after ajax.reload()?

How can i prevent DT from drawing after ajax.reload()?

TheFreemanTheFreeman Posts: 4Questions: 1Answers: 0
edited November 2016 in Free community support

Hi,

i´m using DataTables 1.10.12 for my project.
There the data is loaded by ajax and should be reloaded by interval.
My Problem is, that, if the server sends no fresh data, i cannot prevent DT from go forther after ajax.reload().

setInterval( function()
    {
        var url = global_datatable_ajax_url + "?version=" + global_datatable_version;

        table.ajax.url(url);
        table.ajax.reload(function(json){/* THIS DOES NOTHING */}, false);
    }, 10000 );

I tried the ajax.reload() with a callback. But it is not fired and DT tries to proccess the loaded data.

Can you help me to manage that issue?

best regards.

Answers

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin

    You can't. If the server returns no data, there is no data for the table to display :smile:.

    What you would need to do is make your own Ajax call to get the data, then use clear() and rows.add() to first clear out the old data and then add the new rows. You could add a logic check around that to check that there is actually data to display.

    Allan

  • TheFreemanTheFreeman Posts: 4Questions: 1Answers: 0

    :-) No, the server returns Data, but there is no table-data.
    Because i check on the server, if the data ´has changed.
    If not, there is no data to send.

    Is there no way to use the DT api to solve the porblem?

  • TheFreemanTheFreeman Posts: 4Questions: 1Answers: 0

    Why is the reload-callback not fired?

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin

    No - you need to make your own Ajax call and use the API methods I mentioned above if the server doesn't return any data to be displayed in the table. Its just three or four lines rather than a single API call :smile:.

    Why is the reload-callback not fired?

    It should be: example.

    Allan

  • TheFreemanTheFreeman Posts: 4Questions: 1Answers: 0

    Hi Allan.
    Thank you so much for your great support.
    I made me a proper solution with an extern call:

    /**
        Checks received data from api, if the included version number 
        equals the current. If not, the table-data object is cleared
        and new data is added. After that the table is redrawed.
        
        Parameters:
            @json   [JSON-DATA] from api call
    */
    function list_check_reloaded_data(json)
    {
        if( global_datatable_version == json.data.version )
            return;
        
        var list = json.data.cells;
        
        if( list.length === 0 )
            return;
        
        global_datatable_version = json.data.version;
        global_datatable_data = json.data;
        
        global_datatable_obj.clear();
        global_datatable_obj.rows.add(list);
        global_datatable_obj.draw();
    }
    
    
    // List data auto reload
        setInterval( function()
        {
            var url = global_datatable_ajax_url + "?version=" + global_datatable_version;
            api_call(url, list_check_reloaded_data);
        }, global_common_config.list.reload_interval );
    

    That works for me :smiley:

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin

    Perfect - nice one!

    Allan

This discussion has been closed.