is there a way to define a function in datatables order?

is there a way to define a function in datatables order?

chinovskichinovski Posts: 4Questions: 2Answers: 0

Actually, I have 2 tables (pending and published) using datatables on the same page.
The order API , as I know, allows to choose only columns and sorting direction.
My data are sorted now depending on closing date for both tables

order : [ 5, "desc" ]

Is there a way to choose the sorting column depending on the result of a function? For exemple:

order : function(){
    if(tab == 'pending'){
        return [4, "desc"]; // sort publication date column
    } else {
        return [5, "desc"]; // sort closing date column
    }
}

Thank you!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,322Questions: 26Answers: 4,773
    Answer ✓

    You can use the order() API inside initComplete to set the order based on the conditions.

    Kevin

  • chinovskichinovski Posts: 4Questions: 2Answers: 0

    @kthorngren

    Thank you Kevin

    I added table.order([4, 'asc']).draw(); inside initComplete as you said ant it worked.

This discussion has been closed.