How to sort datatable rows based on a background color?

How to sort datatable rows based on a background color?

stoustou Posts: 2Questions: 1Answers: 0

Hello,

My datatable rows (tr) are displayed with a background color based on some criteria (red, green and white).
I want to have an organised display (all red rows together first, then green and white rows) when my page show up.
I read that Datatables proposes classes like 'sorting_1' and 'sorting_2' to sort rows but I didn't find a way to apply these classes to my code.
Is these classes are integrated to datatables.min.css or I should download other files?
Is there another way to sort my rows?

Thank you.

Answers

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921

    With Orthogonal data you can set filtering values for "Red", "Green", etc to sort in the order you like. I would use columns.render for the sort type to do this. Might look something like this:

        render: function ( data, type, row ) {
            // If display or filter data is requested, format the date
            if ( type === 'sort' {
                if (data === "Red") {
                    return 0;
                } els if (data === 'Green') {
                    return 1;
                } else if .......
                    ........
                }
                return data;   // This is returned if all the if statements fail
    
            }
            return data;   // This is returned if the type is other thatn sort.
        }
    

    You will find a similar example on the ORthogonal data ldocs.

    Kevin

  • stoustou Posts: 2Questions: 1Answers: 0

    Thanks for your reply.
    I think that 'render' and 'data' are applied to columns?
    I want to manipulate the rows data..

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921

    I think that 'render' and 'data' are applied to columns?
    I want to manipulate the rows data..

    Please describe in more detail what you are wanting to do. Can you put together a simple example so we can see exactly what you have so we can help?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    I want to manipulate the rows data..

    I want to have an organised display (all red rows together first, then green and white rows) when my page show up.

    Yep, but the ordering is done on the column, so that's why you would use columns.render as Kevin suggested.

    Also, as he said, please give more details, or a test case with step by step instructions, if that doesn't help.

This discussion has been closed.