Table Row Conditional Styling?

Table Row Conditional Styling?

Drum998Drum998 Posts: 14Questions: 6Answers: 0

Hi,

Is it possible to alter the styling of a row in the datatable depending on the data in one of its cells? I'm trying to colour the table rows according to a code in the incoming data. Is this possible? If so, could someone please point me in the right direction?

Thanks

This question has an accepted answers - jump to answer

Answers

  • F12MagicF12Magic Posts: 109Questions: 0Answers: 28
    edited October 2016 Answer ✓

    Hello @Drum998,
    since this question pops up now and then, I made an example on codepen of how this can be done. Here's the link to the example.
    Codepen: Conditional row styling

    Think it's also possible with the columns.createdCell , but I haven't tested that out.

  • Drum998Drum998 Posts: 14Questions: 6Answers: 0

    You, sir, are a gentleman and a scholar! Thank you!

  • F12MagicF12Magic Posts: 109Questions: 0Answers: 28

    You're welcome.

  • Drum998Drum998 Posts: 14Questions: 6Answers: 0
    edited October 2016

    I tried the method pointed to above by F12Magic and had some limited success, but I was seeing some odd results in my actual application using json data and adding further colours. I ended up using another method using createdRow and I am adding it here for future reference.

    createdRow: function( row, data, dataIndex ) {
        if ( data['jobStatus'] == "red" ) {
            $(row).addClass( 'lightRed' );
        }else if(data['jobStatus'] == "green"){
            $(row).addClass( 'lightGreen' );
        }else if(data['jobStatus'] == "amber"){
            $(row).addClass( 'lightAmber' );
        }
    }
    

    This method has the possible advantage that the field supplying the condition need not be visible.

This discussion has been closed.