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

Asked

Viewed 119 times

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 executing 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 ?

1 answer

2

columnDefs is an option that allows you to set properties on startup of the dataTables plugin, but is not relived after updates to the table occur.

For even after calling Ajax the CSS class or other properties are preserved, you must make use of the option createdRow, which is executed whenever a row is created in the table, either on startup or after table manipulation:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.