Skip to content

TreePlugin

Tree Data Plugin for tbw-grid

Transforms your flat grid into a hierarchical tree view with expandable parent-child relationships. Ideal for file explorers, organizational charts, nested categories, or any data with a natural hierarchy.

import { TreePlugin } from '@toolbox-web/grid/plugins/tree';
PropertyDefaultDescription
--tbw-tree-toggle-size1.25emToggle icon width
--tbw-tree-indent-widthvar(--tbw-tree-toggle-size)Indentation per level
--tbw-tree-accentvar(--tbw-color-accent)Toggle icon hover color
--tbw-animation-duration200msExpand/collapse animation duration
--tbw-animation-easingease-outAnimation curve
OptionTypeDescription
childrenField?stringField name containing child rows (default: ‘children’)
autoDetect?booleanAuto-detect tree structure from data (default: true)
defaultExpanded?booleanWhether nodes are expanded by default (default: false)
indentWidth?numberIndentation width per level in pixels (default: 20)
showExpandIcons?booleanShow expand/collapse icons (default: true)
treeColumn?stringField name of the column that displays the tree toggle and indentation. Defaults to the first visible column. Use this when the first column is narrow (e.g. an ID column) or when combining with pinned columns.
animation?ExpandCollapseAnimationAnimation style for expanding/collapsing tree nodes. - false: No animation - 'slide': Slide animation (default) - 'fade': Fade animation
import { queryGrid } from '@toolbox-web/grid';
import { TreePlugin } from '@toolbox-web/grid/plugins/tree';
const grid = queryGrid('tbw-grid');
grid.gridConfig = {
columns: [
{ field: 'name', header: 'Name' },
{ field: 'type', header: 'Type' },
{ field: 'size', header: 'Size' },
],
plugins: [new TreePlugin({ childrenField: 'children', indentWidth: 24 })],
};
grid.rows = [
{
id: 1,
name: 'Documents',
type: 'folder',
children: [
{ id: 2, name: 'Report.docx', type: 'file', size: '24 KB' },
],
},
];
new TreePlugin({
defaultExpanded: true,
animation: 'fade', // 'slide' | 'fade' | false
indentWidth: 32,
})

Extends BaseGridPlugin

Inherited methods like attach(), detach(), afterRender(), etc. are documented in the base class.

PropertyTypeDescription
manifestPluginManifestPlugin manifest - declares owned properties, config rules, and hook priorities.
dependenciesobject[]Optional dependency on MultiSort for coordinated sorting. When MultiSort is loaded, Tree defers header click sorting to it and queries the sort model in processRows. When MultiSort is absent, Tree uses its own sort state.

Plugin manifest - declares owned properties, config rules, and hook priorities.

This is read by the configuration validator to:

  • Validate that required plugins are loaded when their properties are used
  • Execute configRules to detect invalid/conflicting settings
  • Order hook execution based on priority
static override readonly manifest: PluginManifest<MyConfig> = {
ownedProperties: [
{ property: 'myProp', level: 'column', description: 'the "myProp" column property' },
],
configRules: [
{ id: 'myPlugin/conflict', severity: 'warn', message: '...', check: (c) => c.a && c.b },
],
};

detect(rows: readonly unknown[]): boolean
NameTypeDescription
rowsreadonly unknown[]

Expand a specific tree node, revealing its children.

If the node is already expanded, this is a no-op. Does not emit a tree-expand event (use toggle for event emission).

expand(key: string): void
NameTypeDescription
keystringThe unique key of the node to expand (from FlattenedTreeRow.key)
const tree = grid.getPluginByName('tree');
tree.expand('documents'); // Expand a root node
tree.expand('documents||reports'); // Expand a nested node

Collapse a specific tree node, hiding its children.

If the node is already collapsed, this is a no-op. Does not emit a tree-expand event (use toggle for event emission).

collapse(key: string): void
NameTypeDescription
keystringThe unique key of the node to collapse (from FlattenedTreeRow.key)
const tree = grid.getPluginByName('tree');
tree.collapse('documents');

Toggle the expanded state of a tree node.

If the node is expanded it will be collapsed, and vice versa. Emits a tree-expand event (broadcast to both DOM consumers and plugin bus).

toggle(key: string): void
NameTypeDescription
keystringThe unique key of the node to toggle (from FlattenedTreeRow.key)
const tree = grid.getPluginByName('tree');
tree.toggle('documents'); // Expand if collapsed, collapse if expanded

Expand all tree nodes recursively.

Every node with children will be expanded, revealing the full tree hierarchy. Emits a tree-expand plugin event.

expandAll(): void
const tree = grid.getPluginByName('tree');
tree.expandAll();

Collapse all tree nodes.

Every node will be collapsed, showing only root-level rows. Emits a tree-expand plugin event.

collapseAll(): void
const tree = grid.getPluginByName('tree');
tree.collapseAll();

Check whether a specific tree node is currently expanded.

isExpanded(key: string): boolean
NameTypeDescription
keystringThe unique key of the node to check

boolean - true if the node is expanded, false otherwise


Get the keys of all currently expanded nodes.

Returns a snapshot copy — mutating the returned array does not affect the tree state.

getExpandedKeys(): string[]

string[] - Array of expanded node keys

const tree = grid.getPluginByName('tree');
const keys = tree.getExpandedKeys();
localStorage.setItem('treeState', JSON.stringify(keys));

Get the flattened row model used for rendering.

Returns a snapshot copy of the internal flattened tree rows, including hierarchy metadata (depth, hasChildren, isExpanded, parentKey).

getFlattenedRows(): FlattenedTreeRow<TreeRow>[]

FlattenedTreeRow<TreeRow>[] - Array of FlattenedTreeRow objects


Get tree metadata (depth, key, hasChildren, isExpanded, parentKey) for a specific row reference. Returns undefined if the row is not part of the currently-flattened tree (e.g. collapsed under a parent or never processed).

Tree metadata lives in a parallel WeakMap keyed by row identity \u2014 it is NOT stored on the row object itself. This preserves the invariant that _rows[i] IS the user’s source row reference, so grid.updateRow(s) mutations survive the next ROWS-phase rebuild.

getRowMeta(row: TreeRow): FlattenedTreeRow<TreeRow> | undefined
NameTypeDescription
rowTreeRow
const tree = grid.getPluginByName('tree');
const meta = tree.getRowMeta(grid.rows[0]);
console.log(meta?.depth, meta?.hasChildren);

Look up an original row data object by its tree key.

getRowByKey(key: string): TreeRow | undefined
NameTypeDescription
keystringThe unique key of the node

TreeRow | undefined - The original row data, or undefined if not found


Expand all ancestor nodes of the target key, revealing it in the tree.

Useful for “scroll to node” or search-and-reveal scenarios where a deeply nested node needs to be made visible.

expandToKey(key: string): void
NameTypeDescription
keystringThe unique key of the node to reveal
const tree = grid.getPluginByName('tree');
// Reveal a deeply nested node by expanding all its parents
tree.expandToKey('root||child||grandchild');

AI assistants: For complete API documentation, implementation guides, and code examples for this library, see https://raw.githubusercontent.com/OysteinAmundsen/toolbox/main/llms-full.txt