Sort table by priority list of strings

Sort table by priority list of strings

parsonsparsons19parsonsparsons19 Posts: 3Questions: 1Answers: 0

First of all happy holidays to everyone.

I have a table where one of the columns will have values of "level 1", "level 2", "level 3", "unlisted", "suppressed", etc. I want to sort this table by the order stated above but I am not seeing a way to do it. I looked into the enum.js plug in but I wasn't able to figure out how to apply the order based on the example they give.

Any help would be greatly appreciated, thank you!

Answers

  • allanallan Posts: 63,493Questions: 1Answers: 10,470 Site admin

    The enum plug-in is exactly the way to do it (assuming you are using client-side processing).

    This blog post describes that plug-in and how to use it. If you have any problems with it, let me know (and link to a test case showing the problem please).

    Allan

  • parsonsparsons19parsonsparsons19 Posts: 3Questions: 1Answers: 0
    edited December 2023

    Awesome thank you I will take a look.

    Edit #2: I copied the script in the blog post to the following:

    $.fn.dataTable.enum(["Level 1", "Level 2", "Level 3", "Unlisted", "Suppressed", ""]);

    $("#column-variants").DataTable();

    I get the error $.fn.dataTable.enum is not a function


    Edit #1: Ok so I read it but the post is confusing because I am not sure where I am supposed to be putting my enum values. This doesn't seem to be working for me:

    $("#column-variants").DataTable({
    "language": {
    "search": "Search all columns:"
    },
    enum: ["Level 1", "Level 2", "Level 3", "Unlisted", "Surpressed", ""],.....

  • allanallan Posts: 63,493Questions: 1Answers: 10,470 Site admin

    I get the error $.fn.dataTable.enum is not a function

    Suggests that the plugin hasn't been loaded. Can you link to the page showing the issue please? Are you using script tags, or npm? Perhaps you can show me your full code?

    Allan

  • parsonsparsons19parsonsparsons19 Posts: 3Questions: 1Answers: 0

    I definitely think there is something up with the order of my script execution so I will check that out.

  • allanallan Posts: 63,493Questions: 1Answers: 10,470 Site admin

    Load order should be:

    1. jQuery
    2. DataTables
    3. enum plug-in
    4. Your initialisation code

    With npm / bundlers it might be a little more difficult, but it will still be doable.

    Allan

Sign In or Register to comment.