Lightweight, composable tree component for Ember.
- Ember.js v6.8 or above
- Ember CLI v6.8 or above
- Node.js v22 or above
Supported versions follow the active Ember LTS releases.
ember install ember-simple-treeThe styles are included with the component.
All of them use the tree-* and toggle-icon classes, which you can override in your own CSS.
Basic example:
Standard example:
Upgrading from an older version? See UPGRADING.md.
Returns: node
Fired when a checkbox state changes.
Returns: node
Fired on right click. When set, the browser's context menu is not shown.
Returns: node
Fired when a mouse enters the node.
Returns: node
Fired when a mouse leaves the node.
Returns: node
Fired when a node is selected.
Default: false
Accepts: boolean
Displays a checkbox for each node, reflecting model.isChecked.
In the block form, render <node.Checkbox /> instead, since checkable has no effect there.
Default: undefined
Accepts: id
Applies the tree-chosen class (bold by default) to the node whose model.id matches.
When the tree is first rendered, the ancestors of that node are expanded so it's visible.
Default: 0
Accepts: number
Expands the tree to a given depth when it's first rendered.
0 will not expand the tree at all, -1 will fully expand a tree, a positive number will expand a tree to the given depth.
Default: false
Accepts: boolean
When enabled, checking a node also checks all of its descendants, and each parent is checked,
unchecked or indeterminate based on its children. This also applies when onSelect changes isChecked.
You may optionally pass a block to the Tree component to render each node area with custom HTML.
The block receives a node object with:
model: the nodeToggle: the expand/collapse toggle, ready to render as<node.Toggle />Checkbox: the checkbox, ready to render as<node.Checkbox />toggleExpand: the action to expand or collapse the node, for your own controlstoggleCheck: the action to check or uncheck the node, for your own controls
Default: ▼
Use the <:expandedIcon> named block on Tree to customize the expanded icon:
Default: ▶
Use the <:collapsedIcon> named block on Tree to customize the collapsed icon:
The icon blocks on Tree only apply when it renders the toggle itself. In the block form you
render the toggle, so pass the icon blocks to node.Toggle instead:
buildTree creates the model from a flat list, using id and parentId for the relationships:
import { buildTree } from 'ember-simple-tree/utils/tree';
buildTree([
{ id: 1, name: 'Root' },
{ id: 2, name: 'Child', parentId: 1 },
]);If your records use other properties for id or name, pass valueKey or labelKey. parentId must reference the value of valueKey:
buildTree(records, { labelKey: 'title', valueKey: 'slug' });The component uses recursion to display the tree. The model requires specific properties to properly function:
id- unique identifiername-stringused to display a nodechildren-arrayof other nodesisChecked-booleanused for checkbox stateisDisabled-booleanused to disable actions on a node (onSelect/onCheck)isExpanded-booleanused to expand a node (children)isIndeterminate-booleanused for checkbox "indeterminate" stateisSelected-booleanset while the node is hoveredisVisible-booleanused to display or hide a node
[{
id: 0,
name: 'Root',
isExpanded: true,
isSelected: false,
isVisible: true,
children: [
{
id: 1,
name: 'First Child',
isExpanded: true,
isSelected: false,
isVisible: true,
children: []
},
{
id: 2,
name: 'Second Child',
isExpanded: true,
isSelected: false,
isVisible: true,
children: [
{
id: 3,
name: 'First Grand Child',
isExpanded: true,
isSelected: true,
isVisible: true,
children: []
}
]
}
]
}]A utility class is provided to convert a flat structure into a tree structure and vice-versa.
This project is licensed under the MIT License.