How to format cells depending on conditions

How to format cells depending on conditions

tlasrichtlasrich Posts: 4Questions: 1Answers: 0

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

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited December 2018 Answer ✓

    You can use createdRow or rowCallback for this. The docs have some examples to get you started.

    Kevin

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @tlasrich ,

    As Kevin said, those callbacks are the way to go. Here's a live example that may help,

    Cheers,

    Colin

  • tlasrichtlasrich Posts: 4Questions: 1Answers: 0

    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

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Is it possible to fire it again after changing some of the data of a row?

    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() or cell().data() to update the data or use row().invalidate() or cell().invalidate() (followed by draw()) to have Datatables refresh its data cache and redraw the table.

    Kevin

  • tlasrichtlasrich Posts: 4Questions: 1Answers: 0

    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

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    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

  • tlasrichtlasrich Posts: 4Questions: 1Answers: 0

    ok, I checked everything again and read your answers onesmore. And after changing createdRow to rowCallback everything works fine.

    Thanks for your patience.

    Thorsten

This discussion has been closed.