Search Results - Sort instead of filter
Search Results - Sort instead of filter
mdenning
Posts: 4Questions: 1Answers: 0
Is it possible to sort search results rather than filter?
So rather than filtering and thus hiding correct search results, all the original datatable contents would
remain in place, with the "match hits" on top.
So, In a table with contents
1. A
2. BB
3. AA
Results of a search of "A" would be
1. A
2. AA
3. BB
This discussion has been closed.
Answers
Basically any traditional matches would be on the top,
with non-matches remaining in the datatable, but with
a different styling applied.
Hi @mdenning ,
I've been trying to think of a tidy way to do this, but I don't think it would be easy, and you would need to have
paging
andordering
disabled.What you could do is have a custom search input box, which does the
search()
to only show the matching records at the top. You would then get all the rows withrows()
, and manually add those back into the table into the HTML that don't match the search conditions.It would be messy, and any table draw would remove those rows, but it could be an avenue to investigate if it's important.
Cheers,
Colin
Thanks for providing those paths Colin,
I had vague notions of appending to the search results but wanted to check to make sure there wasn't a non-obvious easy way.
Sounded like an interesting problem so I took a look. I tried a different approach that may work. Not fully tested and will probably need to be changed to meet your needs. Likely you can improve the efficiency.
http://live.datatables.net/koyemeru/2/edit
It doesn't perform a search. It uses a text input and the
filter()
API to find rows that match the text input. It uses a modified version of this example for row indexes:https://datatables.net/examples/api/counter_columns.html
Essentially if the
filter()
API finds a match it sets the index column data to 0. Also it will set the index column as the priority order. This way all indexes with 0 (matches) will be at the top. Then, using the search and order events, it will set the index based on the table order unless the index column data is 0.I put some comments in and hopefully it makes sense. You can add console.log output to understand better what its doing. Also you can hide the index column (uncomment
columnDefs
).You can use
rowCallback
to highlight the matched rows. For this test case you need to type the full name, case sensitive, for the match,Cedric Kelly
for example. Type into theFind
input.Have fun and hope it helps.
Kevin
Thanks Kevin,
I've been away from this portion of the project for some time but I will attempt to adapt your solution and post back results in the event of a remarkable success.
Appreciate it.