A browser-ready tree library that can efficiently display a large tree with smooth scrolling.
Demo: http://cheton.github.io/infinite-tree
Features
- High performance infinite scroll with large data set
- Customizable renderer to render the tree in any form
- Load nodes on demand
- Native HTML5 drag and drop API
- A rich set of APIs
- No jQuery
Browser Support
![]() Chrome |
![]() Edge |
![]() Firefox |
![]() IE |
![]() Opera |
![]() Safari |
---|---|---|---|---|---|
Yes | Yes | Yes | 8+ | Yes | Yes |
Need to include es5-shim polyfill for IE8
React Support
Check out react-infinite-tree at https://github.com/cheton/react-infinite-tree.
Installation
npm install --save infinite-tree
Usage
const InfiniteTree = ; // when using webpack and browserify; const data = id: 'fruit' name: 'Fruit' children: id: 'apple' name: 'Apple' id: 'banana' name: 'Banana' children: id: 'cherry' name: 'Cherry' loadOnDemand: true ; const tree = el: document data: data autoOpen: true // Defaults to false droppable: // Defaults to false hoverClass: 'infinite-tree-droppable-hover' { return true; } { } { if !parentNode && parentNodeloadOnDemand return true; return false; } { // Loading... const nodes = ; nodeslength = 1000; for let i = 0; i < nodeslength; ++i nodesi = id: `.` name: `.` loadOnDemand: true ; ; } nodeIdAttr: 'data-id' // the node id attribute { // Customizable renderer return '<div data-id="<node-id>" class="infinite-tree-item">' + nodename + '</div>'; } { // Determine if the node is selectable if !node || node === tree return false; // Prevent from deselecting the current node return true; };
Functions Usage
const node = tree;// → Node { id: 'fruit', ... }tree;// → trueconsole;// → Node { id: 'apple', ... }console;// → Node { id: 'banana', ... }console;// → null
Events Usage
Learn more: Events
tree;tree;tree;tree;tree;tree;tree;tree;tree;tree;tree;tree;tree;tree;tree;tree;
API Documentation
FAQ
Index
- Creating tree nodes with checkboxes
- How to attach click event listeners to nodes?
- How to use keyboard shortcuts to navigate through nodes?
- How to filter nodes?
- How to select multiple nodes using the ctrl key (or meta key)?
Creating tree nodes with checkboxes
Sets the checked attribute in your rowRenderer:
const tag = ; const checkbox = ;
In your tree, add 'click', 'contentDidUpdate', 'clusterDidChange' event listeners as below:
// `indeterminate` doesn't have a DOM attribute equivalent, so you need to update DOM on the fly.const updateIndeterminateState = { const checkboxes = treecontentElement; for let i = 0; i < checkboxeslength; ++i const checkbox = checkboxesi; if checkbox checkboxindeterminate = true; else checkboxindeterminate = false; }; tree; tree; tree;
How to attach click event listeners to nodes?
const el = document;const tree = el /* options */ ; tree
Event delegation with jQuery:
const el = document;const tree = el /* options */ ; // jQuery;
How to use keyboard shortcuts to navigate through nodes?
tree;
How to filter nodes?
In your row renderer, returns undefined or an empty string to filter out unwanted nodes (i.e. node.state.filtered === false
):
; const renderer = { if nodestatefiltered === false return; // Do something return ;};
Usage
tree
Use a string or a function to test each node of the tree. Otherwise, it will render nothing after filtering (e.g. tree.filter(), tree.filter(null), tree.flter(0), tree.filter({}), etc.). If the predicate is an empty string, all nodes will be filtered. If the predicate is a function, returns true to keep the node, false otherwise.
Filter by string
const keyword = 'text-to-filter';const filterOptions = caseSensitive: false exactMatch: false filterPath: 'props.name' // Defaults to 'name' includeAncestors: true includeDescendants: true;tree;
Filter by function
const keyword = 'text-to-filter';const filterOptions = includeAncestors: true includeDescendants: true;tree;
Turn off filter
Calls tree.unfilter()
to turn off filter.
tree;
How to select multiple nodes using the ctrl key (or meta key)?
You need to maintain an array of selected nodes by yourself. See below for details:
let selectedNodes = ;tree;
License
MIT