Finding number of matches per row from search/filter API

Finding number of matches per row from search/filter API

rixyrixy Posts: 2Questions: 1Answers: 0

Hi all,

I've been using DataTables for years now and it's an essential part of my toolbox! Amazingly useful.

I'm currently building in a filter which works well but I'm trying to get the following information from it so I can construct a way to organise the rows based on the result. Here goes:

When a filter/search is performed (I supply it regex), I would like to hook in somewhere and return the number of matches per row/column so I can score the row based on this and the rows with the most number of matches for the words searched will bump to the top of the table.

So for example, in this case, the user has typed in a new idea. I've cleaned out some stop words then I pass the cleaned string to the filter/search as regex. It's found 4 items which contain any of the words "Supplier", "Nice" or "Approved".

The 4th result (bottom row) has been matched with "Supplier" 3 times and "Approved" 1 time so a total of 4 matches.

The other rows have fewer matches.

I'll be just fine coding the way it handles the results but the only bit of info I need is the total number of matches per row so I can score them and sort the table appropriately.

I could write a function to search the table and do this but then I'm going to be doing 2 searches effectively.

The command I am running is:
itemTable.search(newSearchString, true, false).draw();

as an example the newSearchString would look like:

**\b(?:Supplier|nice|approved) **

Here is an example of the code in action:

live.datatables.net/cidofola/1/edit

Thank you in advance for any help/pointers :smile:

Rixy

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    Answer ✓

    I would look at creating an Ordering Plugin for this. I updated your example with a very simple example:
    http://live.datatables.net/cidofola/2/edit

    I left it to you to write the code to get the search input value and to build an array of values. You will then need to loop through the values to count how many occur in the data and keep a running counter.

    Kevin

  • rixyrixy Posts: 2Questions: 1Answers: 0

    Wow nice mate, such a quick response and spot on. Thank you so much!!

This discussion has been closed.