Highlighting Search Strings

Highlighting Search Strings

silkspinsilkspin Posts: 152Questions: 34Answers: 5

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

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955

    This code is doing the highlighting:

    function highlight( body, table )
    {
        // Removing the old highlighting first
        body.unhighlight();
    
        // Don't highlight the "not found" row, so we get the rows using the api
        if ( table.rows( { filter: 'applied' } ).data().length ) {
            table.columns().every( function () {
                    var column = this;
                    column.nodes().flatten().to$().unhighlight({ className: 'column_highlight' });
                    column.nodes().flatten().to$().highlight( $.trim( column.search() ).split(/\s+/), { className: 'column_highlight' } );
            } );
            body.highlight( $.trim( table.search() ).split(/\s+/) );
        }
    }
    

    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:

    $.trim( table.search() ).replaceAll( '"', '').split(/\s+/)
    

    Kevin

  • silkspinsilkspin Posts: 152Questions: 34Answers: 5

    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?

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955

    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

  • silkspinsilkspin Posts: 152Questions: 34Answers: 5
    edited March 2022

    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.

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955

    If you enter "sales s" it also highlights the single "s" too

    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

  • silkspinsilkspin Posts: 152Questions: 34Answers: 5
    edited March 2022

    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

  • silkspinsilkspin Posts: 152Questions: 34Answers: 5

    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.

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955
    Answer ✓

    Good deal!

    Kevin

  • silkspinsilkspin Posts: 152Questions: 34Answers: 5

    @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

Sign In or Register to comment.