How to format cells depending on conditions
How to format cells depending on conditions
Hello everyone,
I'm new to DataTables and I love it. I started with a simple table with 6 columns. Depending on the values in column 2 and 3 of a row I would like to highlight cell 4 in the same row. I tried to google this, but the only solution I found was a option for column (cssFormat:mark_votes). I read the Reference on datatables.net but couldn't find this option. I also tested it (try & error), but as aspected it didn't work.
I hope I descriped my problem / feature request clearly. Otherwise please give me a short hint and I will try to do better.
Thanks for any ideas.
Best Regards
Thorsten
This question has accepted answers - jump to:
Answers
You can use
createdRow
orrowCallback
for this. The docs have some examples to get you started.Kevin
Hi @tlasrich ,
As Kevin said, those callbacks are the way to go. Here's a live example that may help,
Cheers,
Colin
Thx @colin and @kthorngren for your quick help.
rowCallback was what I was looking for. If I understood the function correctly than rowCallback will be fired after loading the table. Is it possible to fire it again after changing some of the data of a row? The entered changes of values will be displayed in my datatable but not the conditional styling. After a full reload of the page I get the conditional changes.
In the manual I found the drawCallback, but I'm not shure how it could help me with my problem.
Greetz
Thorsten
How are you changing the data?
RowCallback will be fired each time the table is drawn.
If you are not using a Datatables API to change the data then Datatables won't know about the changes. You can either use
row().data()
orcell().data()
to update the data or userow().invalidate()
orcell().invalidate()
(followed bydraw()
) to have Datatables refresh its data cache and redraw the table.Kevin
Hi,
I checked it again, inserting a new row works as expected but changing a row didn't.
Every row has a button to edit, after clicking on it a html form opens and you can edit the values. After submitting the form I use ajax to save the changed values in the database and table.row('#' + auftragsid).data( {...}).draw() to change the values in the datatable. The Values where updated but not the conditional states like changing cell background color if date is in the past.
Thorsten
Hi @tlasrich ,
There's a lot going on there, it would help if we could see it. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
ok, I checked everything again and read your answers onesmore. And after changing createdRow to rowCallback everything works fine.
Thanks for your patience.
Thorsten