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

Search plugin with multi words support (solution in this post) #10

Closed
mbideau opened this issue May 18, 2011 · 4 comments
Closed

Search plugin with multi words support (solution in this post) #10

mbideau opened this issue May 18, 2011 · 4 comments

Comments

@mbideau
Copy link

mbideau commented May 18, 2011

I needed to search for multiple words separated by spaces so I give you my function to add to search plugin (currently the search engine plugin only search for one word).

/*

  • jsTree search plugin

  • Enables both sync and async search on the tree

  • DOES NOT WORK WITH JSON PROGRESSIVE RENDER

    */

(function ($) {

// my function starts here
    $.expr[':'].jstree_contains_multi = function(a,i,m){

    var word, words = [];
    var searchFor = m[3].toLowerCase();
    if(searchFor.indexOf(' ') >= 0) {
        words = searchFor.split(' ');
    }
    else {
        words = [searchFor];
    }
    for (var i=0; i < words.length; i++) {
        word = words[i];
        if((a.textContent || a.innerText || "").toLowerCase().indexOf(word) >= 0) {
            return true;
        }
    }
    return false;

};

// my function ends here
$.expr[':'].jstree_contains = function(a,i,m){

    return (a.textContent || a.innerText || "").toLowerCase().indexOf(m[3].toLowerCase())>=0;

};

That's all

@mbideau
Copy link
Author

mbideau commented May 18, 2011

I've forgotten to trim the input, so the right function (that prevent from selecting everynode in case of extra spaces at the end of search string) :

/*

 * jsTree search plugin

 * Enables both sync and async search on the tree

 * DOES NOT WORK WITH JSON PROGRESSIVE RENDER

 */

(function ($) {

    $.expr[':'].jstree_contains_multi = function(a,i,m){

        var word, words = [];
        var searchFor = m[3].toLowerCase().replace(/^\s+/g,'').replace(/\s+$/g,'');
        if(searchFor.indexOf(' ') >= 0) {
            words = searchFor.split(' ');
        }
        else {
            words = [searchFor];
        }
        for (var i=0; i < words.length; i++) {
            word = words[i];
            if((a.textContent || a.innerText || "").toLowerCase().indexOf(word) >= 0) {
                return true;
            }
        }
        return false;

    };

    $.expr[':'].jstree_contains = function(a,i,m){

        return (a.textContent || a.innerText || "").toLowerCase().indexOf(m[3].toLowerCase())>=0;

    };

@vakata
Copy link
Owner

vakata commented Jul 11, 2011

I will include this with the useful snippets for version 1.0. Thanks :)

@marcselman
Copy link

Better create two different contains functions (contains_all and contains_any):

$.expr[':'].jstree_contains_all = function(a,i,m) {
    var word, words = [];
    var searchFor = m[3].toLowerCase().replace(/^\s+/g,'').replace(/\s+$/g,'');
    if (searchFor.indexOf(' ') >= 0) {
        words = searchFor.split(' ');
    }
    else {
        words = [searchFor];
    }
    for (var i = 0; i < words.length; i++) {
        word = words[i];
        if ((a.textContent || a.innerText || "").toLowerCase().indexOf(word) == -1) {
            return false;
        }
    }
    return true;
};
$.expr[':'].jstree_contains_any = function(a,i,m) {
    var word, words = [];
    var searchFor = m[3].toLowerCase().replace(/^\s+/g,'').replace(/\s+$/g,'');
    if (searchFor.indexOf(' ') >= 0) {
        words = searchFor.split(' ');
    }
    else {
        words = [searchFor];
    }
    for (var i = 0; i < words.length; i++) {
        word = words[i];
        if ((a.textContent || a.innerText || "").toLowerCase().indexOf(word) >= 0) {
            return true;
        }
    }
    return false;
};

@nrpieper
Copy link

Example of contains any working with jstree 3: https://jsfiddle.net/t067rm8d/5/

"search": {
    "case_insensitive": true,
    "show_only_matches": true,
    "search_callback": function(str, node) {

      //search for any of the words entered
      var word, words = [];
      var searchFor = str.toLowerCase().replace(/^\s+/g, '').replace(/\s+$/g, '');
      if (searchFor.indexOf(' ') >= 0) {
        words = searchFor.split(' ');
      } else {
        words = [searchFor];
      }
      for (var i = 0; i < words.length; i++) {
        word = words[i];
        if ((node.text || "").toLowerCase().indexOf(word) >= 0) {
          return true;
        }
      }
      return false;
    }

  },
  plugins: ["search", "wholerow"]`

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

4 participants