srk
srk

Reputation: 5156

jQuery Data table reload

I am using jQuery datatable plugin. I am trying to fetch the record count and based on the count doing some HTML manipulation using jQuery.

So far I have used this code

$('#tb').on('init.dt', function () {
        var totalRecords = table.page.info().recordsTotal;
        if(totalRecords != 0) {
            $('#tb_div').show();
            table.columns.adjust().draw();
        }else{
            $('#tb_div').hide();
            $('#no_rec_msg').show();
        }
    } );

But this init.dt gets executed just once and it doesn't work on table.ajax.reload(); Any API method that would fix this?

Upvotes: 1

Views: 146

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58930

Use xhr event instead that will be fired when an Ajax request is completed.

$('#tb').on('xhr.dt', function () {
   var totalRecords = table.page.info().recordsTotal;
   if(totalRecords != 0) {
      $('#tb_div').show();
      table.columns.adjust().draw();
   } else {
      $('#tb_div').hide();
      $('#no_rec_msg').show();
   }
} );

Upvotes: 1

Related Questions