Skip to content
Martin@MBP edited this page Mar 2, 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: {'dimm' | 'hide'}, default: 'dimm'
    Defines if unmatched nodes are grayed out or hidden.

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}
    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.0.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:

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

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;
  }
}
});
Clone this wiki locally