Programmatically sorting with more than one custom sort function

Programmatically sorting with more than one custom sort function

Luca4k4Luca4k4 Posts: 9Questions: 3Answers: 0

Hi, i do not know how to do that:
I have a table. I have 2 buttons external from the table. I want that the button "x" sorts the column 2 with a custom function and i want that the button "y" sorts the column 2 (the same of before) with another custom function.

Can i do that with Datatables? What is the correct way to procede?

Thank you,
Luca

Answers

  • colincolin Posts: 15,158Questions: 1Answers: 2,587
    edited May 2018

    Hi @Luca4k4 ,

    Yep, you can just use the API search() methods to search the table, something like:

    $('#example').DataTable().column(3).search('fred');
    

    You can call those methods from the button's 'click' handler.

    Cheers,

    Colin

  • Luca4k4Luca4k4 Posts: 9Questions: 3Answers: 0

    Hi, thank you for the answer but i do not need to search over table but to ordering a column in a way given from a custom function (two custom functions in my case)

  • colincolin Posts: 15,158Questions: 1Answers: 2,587

    ah, sorry, misread - ordering is just as easy, see order() - again, this can be initiated in the same handler.

  • Luca4k4Luca4k4 Posts: 9Questions: 3Answers: 0

    Okay, thanks! But... can i have two custom ordering functions for the same column?

    This is the situation i'm looking for:

    Column 2 (Min/Avg values)
    4.5 / 6
    5.3 / 8
    9.4 / 12

    External from the table i've two button that should work in this way:
    - Button 1: it sorts the column 2 by Min values
    - Button 2: it sorts the column 2 by Avg values

  • colincolin Posts: 15,158Questions: 1Answers: 2,587

    Hi @Luca4k4 ,

    Ah, I see what you're saying, so column 2 actually contains two values, gotcha. That would require a special sort, like these plug-ins here. This example is quite straightforward and will help.

    You can only define one data type for the column, but as the examples show, the ordering is applied on demand. Therefore, each of your buttons could set a property/variable some where, and the ordering plugin could then check on that value to determine which one to use and order appropriately.

    I hope that makes sense and gets you going. If not, let me know, and I'll see if I can knock out a prototype over the next few days.

    Cheers,

    Colin

  • colincolin Posts: 15,158Questions: 1Answers: 2,587

    Hi @Luka4k4 ,

    Take a look at this example, this is the sort you were after. Hope that does the trick for you,

    Cheers,

    Colin

This discussion has been closed.