```
You may also supply a toggle handler as a function (rather than an angular
expression) using `ivh-treeview-options` or by setting a global `onToggle`
option. In this case the function will be passed a single object with `ivhNode`
and `ivhTree` properties.
***Demo***: [Toggle Handler](http://jsbin.com/xegari/edit)
### Select/Deselect Handlers
Want to be notified any time a checkbox changes state as the result of a click?
Use the `ivh-treeview-on-cb-change` attribute. Your expression will be evaluated
whenever a node checkbox changes state with the following local variables:
`ivhNode`, the node whose selected state changed; `ivhIsSelected`, the new
selected state of the node; and `ivhTree`, the tree `ivhNode` belongs to.
You may also supply a selected handler as a function (rather than an angular
expression) using `ivh-treeview-options` or by setting a global `onCbChange`
option. In this case the function will be passed a single object with `ivhNode`,
`ivhIsSelected`, and `ivhTree` properties.
Note that programmatic changes to a node's selected state (including selection
change propagation) will not trigger this callback. It is only run for the
actual node clicked on by a user.
```html
```
***Demo***: [Select/Deselect Handler](http://jsbin.com/febexe/edit)
## All the Options
If passing a configuration object is more your style than inlining everything in
the view, that's OK too.
In your fancy controller...
```javascript
this.customOpts = {
useCheckboxes: false,
onToggle: this.awesomeCallback
};
```
In your view...
```html
```
Any option that can be set with `ivhTreeviewOptionsProvider` can be overriden
here.
## Treeview Manager Service
`ivh.treeview` supplies a service, `ivhTreeviewMgr`, for interacting with your
tree data directly.
#### `ivhTreeviewMgr.select(tree, node[, opts][, isSelected])`
Select (or deselect) an item in `tree`, `node` can be either a reference to the
actual tree node or its ID.
We'll use settings registered with `ivhTreeviewOptions` by default, but you can
override any of them with the optional `opts` parameter.
`isSelected` is also optional and defaults to `true` (i.e. the node will be
selected).
When an item is selected each of its children are also selected and the
indeterminate state of each of the node's parents is validated.
***Demo***: [Programmatic select/deselect](http://jsbin.com/kotohu/edit)
#### `ivhTreeviewMgr.selectAll(tree[, opts][, isSelected])`
Like `ivhTreeviewMgr.select` except every node in `tree` is either selected or
deselected.
***Demo***: [Programmatic selectAll/deselectAll](http://jsbin.com/buhife/edit)
#### `ivhTreeviewMgr.selectEach(tree, nodes[, opts][, isSelected])`
Like `ivhTreeviewMgr.select` except an array of nodes (or node IDs) is used.
Each node in `tree` corresponding to one of the passed `nodes` will be selected
or deselected.
***Demo***: [Programmatic selectEach/deselectEach](http://jsbin.com/burigo/edit)
#### `ivhTreeviewMgr.deselect(tree, node[, opts])`
A convenience method, delegates to `ivhTreeviewMgr.select` with `isSelected` set
to `false`.
***Demo***: [Programmatic select/deselect](http://jsbin.com/kotohu/edit)
#### `ivhTreeviewMgr.deselectAll(tree[, opts])`
A convenience method, delegates to `ivhTreeviewMgr.selectAll` with `isSelected`
set to `false`.
***Demo***: [Programmatic selectAll/deselectAll](http://jsbin.com/buhife/edit)
#### `ivhTreeviewMgr.deselectEach(tree, nodes[, opts])`
A convenience method, delegates to `ivhTreeviewMgr.selectEach` with `isSelected`
set to `false`.
***Demo***: [Programmatic selectEach/deselectEach](http://jsbin.com/burigo/edit)
#### `ivhTreeviewMgr.expand(tree, node[, opts][, isExpanded])`
Expand (or collapse) a given `node` in `tree`, again `node` may be an actual
object reference or an ID.
We'll use settings registered with `ivhTreeviewOptions` by default, but you can
override any of them with the optional `opts` parameter.
By default this method will expand the node in question, you may pass `false` as
the last parameter though to collapse the node. Or, just use
`ivhTreeviewMgr.collapse`.
***Demo***: [Programmatic expand/collapse](http://jsbin.com/degofo/edit?html,js,output)
#### `ivhTreeviewMgr.expandRecursive(tree[, node[, opts][, isExpanded]])`
Expand (or collapse) `node` and all its child nodes. Note that you may omit the
`node` parameter (i.e. expand/collapse the entire tree) but only when all other
option parameters are also omitted.
***Demo***: [Programmatic recursive expand/collapse](http://jsbin.com/wugege/edit)
#### `ivhTreeviewMgr.expandTo(tree, node[, opts][, isExpanded])`
Expand (or collapse) all parents of `node`. This may be used to "reveal" a
nested node or to recursively collapse all parents of a node.
***Demo***: [Programmatic reveal/hide](http://jsbin.com/musodi/edit)
#### `ivhTreeviewMgr.collapse(tree, node[, opts])`
A convenience method, delegates to `ivhTreeviewMgr.expand` with `isExpanded`
set to `false`.
#### `ivhTreeviewMgr.collapseRecursive(tree[, node[, opts]])`
A convenience method, delegates to `ivhTreeviewMgr.expandRecursive` with
`isExpanded` set to `false`,
***Demo***: [Programmatic recursive expand/collapse](http://jsbin.com/wugege/edit)
#### `ivhTreeviewMgr.collapseParents(tree, node[, opts])`
A convenience method, delegates to `ivhTreeviewMgr.expandTo` with `isExpanded`
set to `false`.
***Demo***: [Programmatic reveal/hide](http://jsbin.com/musodi/edit)
#### `ivhTreeviewMgr.validate(tree[, opts][, bias])`
Validate a `tree` data store, `bias` is a convenient redundancy for
`opts.defaultSelectedState`.
When validating tree data we look for the first node in each branch which has a
selected state defined that differs from `opts.defaultSelectedState` (or
`bias`). Each of that node's children are updated to match the differing node
and parent indeterminate states are updated.
***Demo***: [Programmatic select/deselect](http://jsbin.com/bexedi/edit)
## Dynamic Changes
Adding and removing tree nodes on the fly is supported. Just keep in mind that
added nodes do not automatically inherit selected states (i.e. checkbox states)
from their parent nodes. Similarly, adding new child nodes does not cause parent
nodes to automatically validate their own selected states. You will typically
want to use `ivhTreeviewMgr.validate` or `ivhTreeviewMgr.select` after adding
new nodes to your tree:
```javascript
// References to the tree, parent node, and children...
var tree = getTree()
, parent = getParent()
, newNodes = [{label: 'Hello'},{label: 'World'}];
// Attach new children to parent node
parent.children = newNodes;
// Force revalidate on tree given parent node's selected status
ivhTreeviewMgr.select(myTree, parent, parent.selected);
```
## Tree Traversal
The internal tree traversal service is exposed as `ivhTreeviewBfs` (bfs -->
breadth first search).
#### `ivhTreeviewBfs(tree[, opts][, cb])`
We perform a breadth first traversal of `tree` applying the function `cb` to
each node as it is reached. `cb` is passed two parameters, the node itself and
an array of parents nodes ordered nearest to farthest. If the `cb` returns
`false` traversal of that branch is stopped.
Note that even if `false` is returned each of `nodes` siblings will still be
traversed. Essentially none of `nodes` children will be added to traversal
queue. All other branches in `tree` will be traversed as normal.
In other words returning `false` tells `ivhTreeviewBfs` to go no deeper in the
current branch only.
***Demo***: [`ivhTreeviewBfs` in
action](http://jsbin.com/wofunu/1/edit?html,js,output)
## Optimizations and Known Limitations
### Performance at Scale
The default node template assumes a reasonable number of tree nodes. As your
tree grows (3k-10k+ nodes) you will likely notice a significant dip in
performance. This can be mitigated by using a custom template with a few easy
tweaks.
**Only process visible nodes** by adding an `ng-if` to the
`ivh-treeview-children` element. This small change will result in significant
performance boosts for large trees as now only the visible nodes (i.e. nodes
with all parents expanded) will be processed. This change will likely be added
to the default template in version 1.1.
**Use Angular's bind-once syntx in a custom template**. The default template
supports angular@1.2.x and so does not leverage the native double-colon syntax
to make one time bindings. By binding once where possible you can trim a large
number of watches from your trees.
### Known Issues
- Creating multiple treeviews within an ngRepeat loops creates an issue where
each treeview accesses the same controller instance after initial load. See
issue #113.
- We use Angular's `filterFilter` for filtering, by default this compares your
filter string with at all object attributes. This directive attaches an
attribute to your tree nodes to track its selected state (e.g. `selected:
false`). If you want your filter to ignore the selection tracking attribute
use an object or function filter. See issue #151.
## Reporting Issues and Getting Help
When reporting an issue please take a moment to reproduce your setup by
modifying our [starter template](http://jsbin.com/wecafa/2/edit). Only make as
many changes as necessary to demonstrate your issue but do comment your added
code.
Please use Stack Overflow for general questions and help with implementation.
## Contributing
Please see our consolidated [contribution
guidelines](https://github.com/iVantage/Contribution-Guidelines).
## Release History
- 2015-11-29 v1.0.2 Allow numeric ids as well as string ids
- 2015-09-23 v1.0.0 Use expressions rather than callbacks for change/toggle
handlers, update default template. See MIGRATING doc for breaking changes.
- 2015-05-06 v0.10.0 Make node templates customizable
- 2015-02-10 v0.9.0 All options are set-able via attributes or config object
- 2015-01-02 v0.8.0 Add ability to expand/collapse nodes programmatically
- 2014-09-21 v0.6.0 Tree accepts nodes added on the fly
- 2014-09-09 v0.3.0 Complete refactor. Directive no longer propagates changes
automatically on programmatic changes, use ivhTreeviewMgr.
- 2014-08-25 v0.2.0 Allow for initial expansion
- 2014-06-20 v0.1.0 Initial release
## License
[MIT license][license], copyright iVantage Health Analytics, Inc.
[license]: https://raw.github.com/iVantage/angular-ivh-treeview/master/LICENSE-MIT
[bootstrap]: http://getbootstrap.com/
[travis-img]: https://travis-ci.org/iVantage/angular-ivh-treeview.svg?branch=master
[travis-link]: https://travis-ci.org/iVantage/angular-ivh-treeview
[trvw-opts]: https://github.com/iVantage/angular-ivh-treeview/blob/master/src/scripts/services/ivh-treeview-options.js#L13-L103