Search code examples
jquerydatatableserver-sideadjustment

How can I adjust the columns after initializing server-side processing datatable?


When my server-side processing datatable is loaded the header is broken. To resolve this, I added table.columns.adjust().draw() to my initComplete function:

var table = $('.table').DataTable({
   "serverSide": true,
   /// more code...
   "initComplete": function(settings, json) {
       table.columns.adjust().draw();
   }
});

This works, but it affected the pagination. The correct page is not displayed anymore. It is set back to to first page. How can I prevent this?


Solution

  • For DataTables version > 1.10.x, you can simply pass false to draw() api which will redraw the table but maintaining current paging position like:

    table.columns.adjust().draw( false );
    

    As mentioned in the docs:

    • paging false
      • the ordering and search will be recalculated and the rows redrawn in their new positions. The paging will not be reset - i.e. the current page will still be shown.