dataTables - Add and preserve CSS class to the last column of each row

4

To add a CSS class to the last column of each row, I use the following method:

var dataTablesOptions = {
  'columnDefs': [
    { targets: -1, className: 'text-center' }
  ],
  // ...
};

$('#minhaTabela').DataTable( dataTablesOptions );

The problem is that after running any action whose data comes from the server, the last <td/> loses the CSS class.

How to preserve the CSS class even after Ajax calls?

    
asked by anonymous 18.10.2017 / 18:32

1 answer

2

columnDefs is an option that allows you to set properties at startup of the plugIn dataTables, but it is not reread after updates occur to the table.

So that even after Ajax calls the CSS class or other properties are preserved, you should make use of the createdRow , which is executed whenever a row is created in the table, either at startup or after manipulation of the table:

var dataTablesOptions = {
  'createdRow': function ( row, data, index ) {
    $(row).find('td:last-child').addClass('text-center');
  },
  // ...
}

$('#minhaTabela').DataTable( dataTablesOptions );
    
18.10.2017 / 18:32