Highlighting Search Strings
Highlighting Search Strings
http://live.datatables.net/sehuviro/11/edit
I've noticed that string searches wrapped in quotes do actually work, but the highlighting breaks so you can't actually see where the results are at a glance.
For example sales as
in the search input shows 3x Sales Assistant with the correct part highlighted. "sales as"
still displays the same 3 results, but the highlighting disappears. Is there a quick fix for this? I have tried the latest versions of dataTables.searchHighlight.min.js
and jquery.highlight.js
.
This question has an accepted answers - jump to answer
Answers
This code is doing the highlighting:
On lines 11 and 13 the search string is trimmed
($treim()
) then split at spaces. You can probably add replaceAll() to remove the spaces, something like this:Kevin
Hi @kthorngren. I just tried that, but it didn't work. http://live.datatables.net/sehuviro/38/
I also tried
.replace '"'/g, '')
to globally replace the quotes and that didn't work either. Are you suggesting I replace the function because I don't know where the code in your example came from?Sorry, the code I mentioned is in the
dataTables.searchHighlight.js
file. I copied the contents of this file into your test case and commented out the include link you had. Updated it with my suggestion and it seems to work.http://live.datatables.net/sehuviro/39/edit
Kevin
Thanks @kthorngren that does now allow quotes to highlight the text. There is a side-effect though. If you enter
"sales s"
it also highlights the single "s" too. That must be independent of the search function and how the highlight.js handles it. They behave in different ways so this might be more complex to fix.That has to do with the
.split(/\s+/)
which is splitting the string into individual words. You can check for leading and trailing quotes and if they exist then don't use the split method. Just remove the quotes.Kevin
I might've just fixed that problem by making
body.highlight( $.trim( table.search() ).replaceAll('"', '').split(/\s+/) );
>body.highlight( $.trim( table.search() ).replaceAll('"', '') );
I'm not sure if that will have other side-effects but I'll do some testing and update later!
http://live.datatables.net/sehuviro/40/edit
Our messages crossed @kthorngren !
Thanks for your help. I'll have a go at trying to do a conditional because I do need the spilt method too when quotes aren't used.
Good deal!
Kevin
@kthorngren I've made a change to it and after testing it seems to work. If a search starts with a quote it doesn't show highlighted individual characters...
http://live.datatables.net/sehuviro/41/edit