Setting a column classname based on value of other column
Setting a column classname based on value of other column
dynasoft
Posts: 446Questions: 69Answers: 3
Hi
I need to do as per above. Can I return and set a different classname based on the value inside one of a DT's columns?
How can this be achieved? Is using the column render method best or can it be best done by iterating all rows via DT's preinit event and how is the column's classname set then?
Here's what I have:
{ data: null,
defaultContent: '',
orderable: false,
render: function ( data, type, row ) {
if (IsTransTypeInvoiceLike(data.TransType) == true)
{
//return and set classname, but how?
}
},
className: 'details-control'
},
or,
var table = $('#tblDataTable1').DataTable();
$('#tblDataTable1')
.on( 'init.dt', function () {
table.rows().eq(0).each( function ( index ) {
var row = table.row( index );
var data = row.data();
if (IsTransTypeInvoiceLike(data.TransType) == true)
{
//return and set classname, but how?
}
} );
}).dataTable();
I'm setting a different classname for the 1st column in this example: https://datatables.net/blog/2019-01-11
Many thanks.
This discussion has been closed.
Answers
I would use rowCallback because it is easier for this use case.
https://datatables.net/reference/option/rowCallback
Something like:
'td:eq(4)' is the column you want to manipulate.
Example from my own coding. Works like a charm for any kind of manipulation:
Very helpful. Thanks a lot.