DataTable set class and background for a TD

时间:2018-04-20 00:41:17

标签: javascript datatables

I am having big issues assigning a custom class to a TD in DataTables in order to change the background color based on the value of it. It seems like the class is getting assigned, but I am not able to get the if-statement working.

Here is my columnDefs

"columnDefs": [ 
    { className: "dt-center test", "targets": [ 7 ] },
  //{ "sClass": "test", "aTargets": [ 7 ] },

    { className: "dt-center", targets: '_all' },
    { className: "dt-justify", targets: '_all'},
    { targets: [0, 1, 2, 3, 7], visible: true},
    { targets: '_all', visible: false },
]

Here is my JavaScript If-statement

$(".test").each((index, element) => {
    if (element.innerText >= 0) element.classList.add("green");
    else element.classList.add("red");
});

My CSS

.green {
    background-color: green !important;
}
.red {
    background-color: red !important;
}

Any help would be very much appreciated on this matter - It has bugged me out for a while now.

0 个答案:

没有答案