angular-datatables icon indicating copy to clipboard operation
angular-datatables copied to clipboard

Adding a class conditionally to a column

Open vinays84 opened this issue 10 years ago • 4 comments

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';
    }
});

vinays84 avatar Jul 12 '16 14:07 vinays84

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)

kevintc avatar Jul 19 '16 09:07 kevintc

withClass() only accepts string, not a function like renderWith does. I'm not sure how className could apply the class conditionally

vinays84 avatar Jul 19 '16 13:07 vinays84

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');
});

l-lin avatar Aug 03 '16 14:08 l-lin

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');
});

vinays84 avatar Aug 03 '16 19:08 vinays84