Change colour of row based on column value

Change colour of row based on column value

Miko12359Miko12359 Posts: 7Questions: 4Answers: 0

Hello,one of the columns in my table contains a due date, i would like to change the colour of the row to red if the due date has already been reached (due date is in the past), if it has not been reached then the colour should change to green.
I am using php to load the data from the tables if that helps.
Thanks

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954

    Use one of the following to change the color based on the cell data:

    Kevin

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

    Hi @Miko12359 ,

    There are several threads in the forum discussing this, such as this one here.

    Cheers,

    Colin

  • Miko12359Miko12359 Posts: 7Questions: 4Answers: 0

    <script type ="text/javascript"> $(document).ready(function() { const date = new Date(); const formattedDate = date.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric' }).replace(/ /g, '-'); $('#myTable').dataTable( { "createdRow": function( row, data, dataIndex){ if( data[6] = formattedDate){ $(row).addClass('red'); } } }); }); </script>

    so i have tried this, it only works if the current date is an exact match, for example the current date is 13-Nov-2018, so only that row is coloured which is fine, but if i try to change:

    if( data[6] =  formattedDate){
    

    to

    if( data[6] <=  formattedDate){
    

    the it doesnt factor the month, it just checks to see if the day is less than current day (checks all rows to see if date is less that 13) it disregards the month, any way to fix this?

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954

    if( data[6] = formattedDate){

    This isn't valid Javascript syntax. You either need to use == or ===.

    Without a test case showing your actual data its hard to say. Maybe you need to convert the data[6] value to a date object to match formattedDate, for example: var colDate = new Date(aData[6]);.

    Please post a simple test case representing your data if you need further help.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

This discussion has been closed.