Absolute custom sorting based on list
Absolute custom sorting based on list
I'm trying to figure out a way to sort my datatable by an absolute list, but I don't see any documentation on how to do that.
What's an absolute list, you ask? Well I just made it up, so I'll explain:
I'm doing a serverside table that uses the column headers to perform filtering of the table based on that column. If the user wants to sort the "name" column and search for four names, "John, Mike, Bob, Andy", he would type "John|Mike|Bob|Andy" into the "Name" column header.
I want the results to be sorted BY those names, in the order that the user typed them.
First, all of the results for John, then all of the results for Mike, then Bob, and lastly, Andy.
Is this possible?
This question has an accepted answers - jump to answer
Answers
Okay, I figured out a solution.
First, I created a new sorting variable in PHP when the user searches for multiple things:
This returns something like this:
$sorting_array['John'] = 1, $sorting_array['Mike'] = 2
etc etc...Then, since DataTables server-side implementation won't allow me to actually SORT on a field that doesn't exist in the database, (or maybe it's the database that won't return values if I try to "sort descending by a column that doesn't exist", if the user chooses to sort by this custom column, I change the sort to something else, run everything, then resort before I display out to the user:
Once the query runs successfully, I add the sorting data manually into the array. My output array that would normally be echoed back to datatables is called
$list
, so:Then I output the newly modified
$list
out back to DataTables, which is none the wiser that I've circumvented its defaults.I hope this helps someone who may be looking to do something similar!
Thanks for posting back. Great to hear you've got this working and thanks for sharing your solution.
Allan