Adding a class conditionally to a column
The DTColumnBuilder API shows that you can use withClass(sClass) and renderWith(function (data, type, row, meta) {}).
How would I apply a class conditionally, for example:
.withClass(function (data, type, row, meta) {
if (row.daysLeft < 0) {
return 'days-past-due';
}
});
Is it working if you add a : else { return 'default-class' } ?
You could try using the className option : https://datatables.net/reference/option/columns.className
DTColumnBuilder.newColumn(null)
.withOption('className', 'tableActions')
.withTitle('actions')
.notSortable()
.renderWith(actionsHtml)
withClass() only accepts string, not a function like renderWith does.
I'm not sure how className could apply the class conditionally
You can't use a function with witClass. If you want to apply a class, you need to use the createdRow property:
DTOptions.fromSource('data.json')
.withOption('createdRow', function(row, data, dataIndex) {
angular.element(row).addClass('foo');
});
Thanks, Louis, that worked for me:
$scope.dtOptions.withOption('createdRow', function(row, data, dataIndex) {
$(row).find('td').filter(function(){ return $(this).text().contains('past due'); }).addClass('days-past-due');
});