A hierarchical list (tree) is a tree-structured catalog for working with large amounts of data.

docs.apptests

When you need to provide the user with convenient access to a large amount of data (hundreds of items). Either the user can organize this data into a tree-structured catalog, or the data already arrives in this form from the system. If these conditions are not met, it is better to use a regular list.

  • Name
  • Item icons (optional)
  • Additional action button for the item (optional)
  • Expand and Collapse buttons
  • Checkbox (optional)
  • Search through items (optional)
tree-select

If a search query is entered, only the following items are shown in the tree:

  • items that match the search query;
  • items that are parents of items found by the search query.

Text matching the search query in the item name is highlighted in bold.

docs.apptests
docs.apptests

Either single or multiple selection is possible (limited at the tree level).

An item can be selected by clicking on it (in any area except the Expand/Collapse icon and the additional action button). To select multiple items with the mouse, use the Ctrl or Shift keys; to select multiple items with the keyboard, use the Space key.

Non-leaf items can be expanded and collapsed by clicking the arrow icon.

docs.apptests

If checkboxes are enabled in the tree list, selecting multiple items is possible by clicking the checkbox.

docs.apptests

The mode is set by multiple: checkbox or keyboard for multiple selection. Single selection is the default, so the way to ask for it is to leave the attribute off entirely. It is a normal input, so [multiple] can be bound and changed at any time. Narrowing a tree that has several nodes selected keeps the first selected one and reports the shortened value; the reported value is a bare value in single selection and an array in multiple selection, so a change of mode changes the shape the form control holds. A tree rendered inside a kbq-tree-select shares its selection model with the select, so the select decides how many nodes may be selected — switching the tree between single and multiple selection there throws, while switching between checkbox and keyboard is allowed.

Note that this applies to kbq-tree-selection and kbq-list-selection only. On Select and kbq-tree-select, multiple still cannot be changed after initialization and throws if you try.

Sometimes an additional action button may be present for an item.

docs.appbadge
testsbadge

There are several variants for multiple item selection. See in examples.

The tree remembers which nodes were expanded and restores the state after a page reload. On by default — use [useStateSaving]="false" to turn it off on a specific component.

Selection is not persisted: it belongs to the form control the tree is bound to.

Expansion is persisted by the value getValue returns for a node, so that value has to be a string, stable across reloads and unique within the tree. A tree on a NestedTreeControl has no getValue and persists nothing, and neither does one rendered into an overlay — kbq-tree-select among them.

Expansion the application performs itself — treeControl.expandAll(), or writing to expansionModel directly — is not persisted until saveState() is called. clearSavedState() removes what is stored.

Keys, storage and expiry work the same for every component that persists — see Saving component state.

Key
Action
TabIf focus is on the component preceding the tree in tab order → Move to the first tree row or the selected row
If focus is on any tree item and it has no additional actions → Move to the next component in tab order after the tree
If focus is on any tree item and it has additional actions → Move to the first additional action
If focus is on an item's additional action → Move to the next additional action (if one exists), otherwise move to the next component in tab order after the tree
Shift + TabIf focus is on any tree item → Move to the previous component in tab order before the tree
If focus is on the component following the tree in tab order → Move to the first tree row or the selected row
→If focus is on a collapsed non-leaf item → Expand the nested items of that item
If focus is on an expanded non-leaf item → Move to its first nested item
If focus is on a leaf item → Nothing happens
←If focus is on an expanded non-leaf item → Collapse the nested items of that item
If focus is on a collapsed non-leaf item → Move to its parent item
If focus is on a leaf item → Move to its parent item
If focus is on a top-level item → Nothing happens
↵If focus is on a tree item → Trigger the primary action of that item
If focus is on a tree item with a checkbox → Nothing happens
If focus is on an additional action → Apply that additional action
SpaceIf focus is on a tree item with a checkbox → Select that tree item (and all nested items, if any)
If focus is on a tree item → Nothing happens
If focus is on an additional action → Apply that additional action
↑ ↓If focus is on a tree item → Move to the next (if it is the last item — nothing happens) / previous tree item (if it is the first item — nothing happens)
PgUpMove focus to the lowest visible item

If the current item is the lowest visible → Scroll the tree down
If the current item is the last in the tree → Nothing happens
PgDnMove focus to the topmost visible item

If the current item is the topmost visible → Scroll the tree up
If the current item is the topmost in the tree → Nothing happens
HomeMove focus to the first tree item
EndMove focus to the last loaded tree item
Ctrl + clickClicking tree items — multiple row selection
Clicking a selected item — the item is deselected
Shift + ↓

Shift + ↑
If focus is on a tree item → Multiple selection of the next / previous tree items (if the row is the first or last — nothing happens)
Ctrl + CIf focus is on tree items → Copy the content of the items
By default, item names are copied. The separator between items is a line break
Ctrl + AIf focus is on any tree item → Select all tree items (including unloaded ones)
Printable charactersMove focus to the next item whose name starts with the typed characters. Typing several characters in quick succession matches them as one word

The tree exposes the structure it renders to assistive technology, so a screen reader announces an item's nesting depth and whether it is expanded, selected or checked.

  • The tree is a role="tree", each row a role="treeitem".
  • The tab stop stays on the tree itself and the rows stay at tabindex="-1", but the row the arrow keys land on does take real focus, and is advertised through aria-activedescendant on top of that.
  • Branch rows carry aria-expanded — including while a filter is active, which disables the chevrons without turning the branches into leaves — and every row carries aria-level.
  • Rows with a checkbox report aria-checked, including mixed for a partially selected branch; rows without one report aria-selected.
  • Expand/collapse chevrons are hidden from assistive technology: their state is already on the row, and the ← and → keys drive them.
  • The tree has no visible label of its own, so give it an accessible name with aria-label, or point aria-labelledby at the element that labels it. A tree inside a kbq-tree-select is labelled by the select and needs neither.

Every expanded item is rendered, with no windowing — a collapsed branch costs nothing, an expanded one costs a DOM row per item. A tree that shows thousands of rows at once should keep its branches collapsed or narrow the data with the search field.

Nodes are tracked by identity by default, so a data source that rebuilds its node objects on every emission — a flattener among them — rebuilds every row, and a rebuilt node comes back collapsed. Give the tree control a trackBy that returns a stable key of a node: the node is then expanded by that key, and the tree re-uses the row of the same key across data updates. Set it before anything is expanded, as a property or in the options, the last argument of FlatTreeControl and NestedTreeControl:

expansionModel then holds the keys rather than the nodes, and the types of the tree, its events and its data source take the key type as one more parameter: KbqTreeSelection<FileFlatNode, string>, KbqTreeFlatDataSource<FileNode, FileFlatNode, string>.

The tree's own trackBy input, a TrackByFunction of the index and the node, overrides the key the rows are re-used by; it does not change the expansion. Without a control trackBy it is the only way to re-use the rows.

Suggestions for improvement
If you found a mistake or want to improve the article, create an issue on GitHub.