Skip to content
Martin edited this page Aug 10, 2016 · 39 revisions

About Fancytree filter extension.

Allow to dimm or hide nodes based on a matching expression.

Note: This plugin might not work as expected if the node titles contain HTML markup.

Options

  • autoApply, type: {boolean}, default: true
    Re-apply last filter if lazy data is loaded.

  • counter, type: {boolean}, default: true
    Show a badge with number of matching child nodes near parent icons.

  • fuzzy, type: {boolean}, default: false
    Match single characters in order, e.g. 'fb' will match 'FooBar'.

  • hideExpandedCounter, type: {boolean}, default: true
    Hide counter badge, when parent is expanded.

  • highlight, type: {boolean}, default: true
    Highlight matches by wrapping inside tags.
    Note that this only works if filter is passed as a string, but not when a custom matcher callback is used.

  • mode, type: {string: 'dimm' | 'hide'}, default: 'dimm'
    Defines if unmatched nodes are grayed out or hidden.

  • nodata, type: {boolean, string, object, function}, default: true
    Display a status node if the filtered tree would be empty.
    A string will be used as title, but also a NodeData object or a callback is accepted.

Events

(n.a.)

Methods

  • {void} tree.clearFilter()
    Reset the filter.

  • {integer} tree.filterBranches(filter, opts)
    Dimm or hide unmatched branches. Matching nodes are displayed together with all descendants.
    filter: {function | string}
    NOTE: another way to achieve this behavior is to use tree.filterNodes() and return "branch" in response of the matcher-callback
    opts {object}, default: {autoExpand: false}.
      autoExpand: Temporarily expand matching node parents, while filter is active.

  • {integer} tree.filterNodes(filter, opts)
    Dimm or hide unmatched nodes.
    filter: {function | string}
    opts {object}, default: {autoExpand: false, leavesOnly: false}.
      autoExpand: Temporarily expand matching node parents, while filter is active.
      leavesOnly: Defines if only end nodes should be considered for matching.

Example

In addition to jQuery, jQuery UI, and Fancytree, include jquery.fancytree.filter.js:

  <script src="//code.jquery.com/jquery-1.12.1.min.js" type="text/javascript"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.min.js" type="text/javascript"></script>
  <link href="skin-win8/ui.fancytree.css" rel="stylesheet" type="text/css">
  <script src="js/jquery.fancytree.js" type="text/javascript"></script>
  <script src="js/jquery.fancytree.filter.js" type="text/javascript"></script>
$("#tree").fancytree({
  extensions: ["filter"],
  filter: {
    autoApply: true, // Re-apply last filter if lazy data is loaded
    counter: true, // Show a badge with number of matching child nodes near parent icons
    hideExpandedCounter: true, // Hide counter badge, when parent is expanded
    mode: "hide"  // "dimm": Grayout unmatched nodes, "hide": remove unmatched nodes
  },
  ...
});
// Case insensitive search for titles containing 'foo':
$("#tree").fancytree("getTree").filterNodes("foo");

// Pass additional options in order to restrict search to end nodes or 
automatically expand matching nodes:
$("#tree").fancytree("getTree").filterNodes("foo", {autoExpand: true, leavesOnly: true});

For more complex searches, we can pass a compare function instead of a string. The matcher callback should return a truthy value in order to display a node.

var rex = new RegExp("foo", "i");

$("#tree").fancytree("getTree").filterNodes(function(node) {
  return rex.test(node.title);
});

$("#tree").fancytree("getTree").filterNodes(function(node) {
  return node.data.customFlag === true;
});

We can match whole branches too, i.e. matching nodes include all descendants:

$("#tree").fancytree("getTree").filterBranches(function(node) {
  return node.key === "abc";
});

The matcher callback can return the special values "skip" and "branch". This allows to mix branch-mode and simple-mode:

var rex = new RegExp("foo", "i");

$("#tree").fancytree("getTree").filterNodes(function(node) {
  var match = rex.test(node.title);

  if( match && node.isFolder() ) {
    return "branch";  // match the whole 'Foo' branch, if it's a folder
  } else if( node.data.ignoreMe ) {
    return "skip";  // don't match anythin inside this branch
  } else {
    return match;  // otherwise match the nodes only
  }
});

Recipes

[Howto] Highlight matches for custom filters

Automatic highlighting only works for string arguments. If a custom matcher callback is used, Fancytree cannot figure out what part caused a match, but we can set 'node.titleWithHighlight' explicitly to a string that contains <mark> tags:

tree.filterNodes(function(node) {
  if( rex.test(node.title) ) {
    node.titleWithHighlight = "<mark>" + node.title + "</mark>";
    return true;
  }
}
});

Another example [found here](https://github.com/mar10/fancytree/issues/551#issuecomment-238780834).
Clone this wiki locally