sort based on array string values

sort based on array string values

itsmesriitsmesri Posts: 9Questions: 3Answers: 0
edited November 2019 in Free community support

I need to sort datatable based on string array. Datatable "Officer Position" column need to sort like array order. example first row "president" 2nd row "Vice President", etc.

var roleArray = ["President", "Vice President", "Membership Chair", "Secretary", "Treasurer", "Member"];

    addTable.row.add([
                searchedItem.FirstName,
                searchedItem.LastName,
                searchedItem.Id,
                            tmpV,
                            searchedItem.DegreeLevel,
                            searchedItem.DegreeProgram,
                               '<button type="button" data_uid="' + searchedItem.Id + '" name="deleteRowBtn" class="btn btn-primary" >Delete</button>',

                getOrderNum(tmpV)
            ]).draw(false);

Answers

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

    Try the enum plugin. I believe all you need to do is pass the array in the order you want it sorted.

    Kevin

  • itsmesriitsmesri Posts: 9Questions: 3Answers: 0
    edited November 2019

    i tried not updating anything

         $.fn.dataTable.enum(["President", "Vice President", "Membership Chair", "Secretary", "Treasurer", "Member"] );
    
         $('#addTable').DataTable();
    
  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921

    It works here:
    http://live.datatables.net/juqivale/1/edit

    Did you load the enum.js?

    Maybe you can update my example to show the issue you are having.

    Kevin

  • itsmesriitsmesri Posts: 9Questions: 3Answers: 0

    http://live.datatables.net/juqivale/2/edit

    Please see your example that I updated. Order suppose to show "President" - On top , "Vice President" - next, "Membership Chair", "Secretary", "Treasurer", "Member" - last to show...

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

    Seems to work when I sort the Position column. Does it not work for you?

    Kevin

  • itsmesriitsmesri Posts: 9Questions: 3Answers: 0

    oops .. Yes. I did not try that way. How should I set sort position column by default?

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

    Use the order option to set the initial order.

    Kevin

  • itsmesriitsmesri Posts: 9Questions: 3Answers: 0

    Works perfect!!! Thank you!1

This discussion has been closed.