adityag28 Posts: 28Questions: 6Answers: 0
Hi, I am using the search bar functionality, but what I want is to show some results first and other later, based on the below requirement.
In this, the search keyword is stage, here I want to show the results with red underline first and then the ones with blue underline because the keyword stage comes first in those.
So how do I display the search results using this criteria?
This question has an accepted answers - jump to answer
@kthorngren can you please take a look into this?
We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
I would take a look at creating a sorting plugin for this.
As far I see it doesn't have what I need. I want to display those records first which have the searched keyword in the starting of the column values and then to display the rest of the records which contains the searched keyword.
What you are looking for is not built into Datatables. A custom sorting solution is needed. The sorting plugin is the way to create a custom sorting solution. Take a look at the published plugins for some examples.
I had some time so I built this example:
In addition to the sorting plugin I created an API that gets the column type. This is used by the
orderevent to instantiate a one time
searchevent handler. The event handler calls
draw()to force Datatables to order the table. Otherwise the table order won't change while searching.
Sort the Position column then type
systeminto the search input to see the results. The search plugin sort code seems to work but I only tested with this limited data set. Will leave it to you to perform more extensive testing.
I really appreciate the effort, thank you very much.
How do I include this api part if I have my own custom search box, please take a look at this link
I have not added the search bar in html, just in jquery. I just want to know how to include the api part in the code, because there is a problem of reinitialisation of the table.
Thanks once again!
Did you follow the troubleshooting steps provided in at the link in the reinitialize error?
You need to combine the two Datatables initializations into a single init as described here.
Thanks, Kevin, the error is resolved now. I have marked your previous post as the answer.
Works really well.
DataTables is really good, I love it. It makes the work so easy. Great work guys!
If I have the sorting applied on a different column, is there a way I could apply the sorting on the _Position _column whenever I search?
Try using the
searchevent and use
order()to set the column order.