Why my datatable is not reloading using ajax.reload()?

Why my datatable is not reloading using ajax.reload()?

p_huynp_huyn Posts: 2Questions: 1Answers: 0

I am using DataTables 1.10 new API, jQuery 1.10.
Here is the Javascript code:

$(document).ready(function() {

var mytable = $('#mytable').DataTable({
    serverSide:true,
    ajax:{
        url:'MyServlet',
        type:'POST'
    },
    // row = [image, screenname, count, message, timestamp, userId]
    columnDefs:[
        {
            targets:0,
            render:function(imgfn, type, row) {
                return html4thumbnail(imgfn, row[5]);
            }
        },
        {
            targets:1,
            render:function(screenname, type, row) {
                return html4summary(screenname, row[2], row[4], row[3], row[5]);
            }
        },
        {visible:false, targets:[2,3,4,5]}
    ],
    paginate:false
});

// Auto refresh every 30 secs
setInterval(function() {
    mytable.ajax.reload();
}, 30000);

});

MyServlet queries a MySql table.
Initially, mytable is populated correctly. It doesn't refresh when the MySql table changes. A browser refresh would repopulate mytable correctly.

Am I missing any detail?

Answers

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    Looks like it should work to me. Can you link to the page so we can take a look please?

    Thanks,
    Allan

  • p_huynp_huyn Posts: 2Questions: 1Answers: 0

    Problem solved. I found someone else who had the same problem. Here is why: in the JSON object my server returns, I used attribute "draw":1. When the client receives this object, the datatable will not using it to reload itself. So don't use "draw":1 if you want to reload your datatable.

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    Ah I see. Yes, the draw counter needs to be updated for each request. I should point out, that since you are using server-side processing, you could just call the draw() method which would redraw the table.

    Allan

This discussion has been closed.