How to change cell backgroung color based on cell value using datatable
How to change cell backgroung color based on cell value using datatable

Request your help, I am trying to change the table cell color based on cell value using datatables using the below code but it does not work, without columnDefs it prints the values in the datatables , but when i use the columnDefs the values are printed but the cell background color does not change
columns: [
{ data: "Status.0", className: "text-sm-center h8 px-0", type: "num" },
{ data: "Status.1", className: "text-sm-center h8 px-0", type: "num" },
{ data: "Status.2", className: "text-sm-center h8 px-0", type: "num" },
{ data: "Status.3", className: "text-sm-center h8 px-0", type: "num" },
{ data: "Status.4", className: "text-sm-center h8 px-0", type: "num" },
{ data: "Status.5", className: "text-sm-center h8 px-0", type: "num" },
{ data: "Status.6", className: "text-sm-center h8 px-0", type: "num" },
{ data: "Status.7", className: "text-sm-center h8 px-0", type: "num" }
],
columnDefs: [ {
targets: [ 0, 1, 2, 3, 4, 5, 6, 7 ],
render: function(data, type, row, meta){
return data = 0 ? '<td class="bg-success view_data" data-toggle="modal"></td>': data;''
return data = 1 ? '<td class="bg-danger view_data" data-toggle="modal">Error</td>': data;
return data = 2 ? '<td class="bg-warning view_dat"' data-toggle="modal">Warnings</td>': data;
return data = 3 ? '<td class="bg-info view_data"></td>': data;
}
}
],
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Tired the procedure in this link https://datatables.net/reference/option/columns.createdCell , still not working
Likely the problem is
data = 0
is an assignment not a comparison. You need to use either==
or===
. If this doesn't help then useconsole.log(data)
inside the render function to see what values you have. Otherwise we will need to see an example so we know what data you have. Please post a link to your page or a test case replicating the problem.https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin