Code Monkey home page Code Monkey logo

Comments (4)

nrpieper avatar nrpieper commented on June 23, 2024 2

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"]`

from jstree.

marcselman avatar marcselman commented on June 23, 2024 1

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;
};

from jstree.

mbideau avatar mbideau commented on June 23, 2024

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;

    };

from jstree.

vakata avatar vakata commented on June 23, 2024

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

from jstree.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.