Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

highlight doesn't work for custom filter #551

Closed
chino23 opened this issue Jan 25, 2016 · 5 comments
Closed

highlight doesn't work for custom filter #551

chino23 opened this issue Jan 25, 2016 · 5 comments

Comments

@chino23
Copy link
Contributor

chino23 commented Jan 25, 2016

When I apply a filter using

tree.filterNodes(function(node){
        if (node.title.indexOf(searchText) === "test"){
               return true;
        }
}, { autoExpand: true })

it works but it's not highlighted anymore. Even if I use the highlight: true option.

When I use

return new RegExp(match, "i").test(node.title);

it doesn't highlight either.

But with the normal passing of just the string without a function

 var test = tree.filterNodes(searchText, { autoExpand: true });

it works. But I need a function to supply a more complex statement.

@mar10
Copy link
Owner

mar10 commented Jan 25, 2016

Automatic highlighting only works for string arguments, because fancytree cannot figure out what part caused a match in custom callbacks.
But you can set 'node.titleWithHighlight' inside your callback to a string that contains <mark> tags, for example.

@mar10 mar10 closed this as completed Jan 25, 2016
@chino23
Copy link
Contributor Author

chino23 commented Feb 5, 2016

Thank you Martin, I got it to work.

@dparreno
Copy link

Hi! I currently have the same issue. Can you give me a sample on how you managed to get it to work? Thanks!

@chino23
Copy link
Contributor Author

chino23 commented Aug 10, 2016

Sure.

See below, there are two different ways to check and mark it.
One uses the titleWithHighlight attribute on the node, as it's just on the title.
The second one is a field in an array, custom_array, and it's checked there as well.
Also one time it's done with regex, the other time it's done with indexOf
So I can have one search to search in both the title and the array.

var replacement = '<mark>$&</mark>';

searchText = searchText.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");

var rex = new RegExp(searchText, 'ig')

var searchElement;

var isMatch = false;

tree.filterNodes(function (node) {
    node.titleWithHighlight = node.title.replace(rex, replacement);

        if (rex.test(node.title) === true) {

         isMatch = true;

        }

    for (var i = 0; i < node.data.custom_array.length; i++) {

        if (node.data.custom_array[i].indexOf(searchText) !== -1) {

            searchElement = $("#" + node.data.custom_id + "_task").find(".tag" + i);

            searchElement.html(node.data.custom_array[i].replace(rex, replacement));

            isMatch = true;

        }

    }

    return isMatch;

}, {autoExpand: true});

EDIT: fixed formatting

@dparreno
Copy link

That helped a lot! Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants