Update column color dynamically
Update column color dynamically
mc2dev
Posts: 33Questions: 6Answers: 0
in DataTables
Hi,
I'm looking for a way to set a column color.
Here, I'm using JQuery to do the trick but if I add a new row, it takes the previous color...
$(tableId)
.find('th, td')
.each(function (i, th) {
if (th.className.includes(col)) { // some conditions
$(th).css('background-color', newColor) // I want to set the DataTable object instead
}
})
Thanks for your support!
Answers
Use the
createdRow
orcolumns.createdCell
to set the color.Kevin
Thank you for your help Kevin but this code doesn't work as expected:
Any ideas of my mistake?
It works here:
http://live.datatables.net/bofagita/1/edit
Please update the test case to show the issue.
Kevin
Hi Kevin,
I can't find a way to solve my problem and my table structure is too big and specific to update your example.
Otherwise, is there a way to set column background color of the DataTable object?
Thank you for your time.
The test case doesn't need to contain your full solution. In fact its better if it doesn't. It just needs to contain a subset of the solution that demonstrates the issues.
You posted 3 lines of code. Do you have them inside either
columnDefs
orcolumns
? If not it won't work.Other options are to set a classname to the column using
columns.classname
. Then use CSS to apply the settings you want to that classname.Oh, and there is
rowCallback
which is similar tocreatedRow
.Kevin