Skip to content

Tree view ​

A tree of nodes: folders, categories, an outline, a site's pages. Nodes can be links, or be selected or checked as form fields. Large trees can load a node's children when it opens.

Treeview demo. Data is embedded and nothing is saved.
php
<?php

use Caelrik\Ui\Components\TreeView;

// A checkable tree: its checkboxes post the checked ids with the form around it.
return TreeView::make('shared-folders')
    ->label('Folders to share')
    ->checkable('folders')
    ->nodes([
        ['id' => 1, 'label' => 'Documents', 'expanded' => true, 'children' => [
            ['id' => 2, 'label' => 'Invoices', 'checked' => true],
            ['id' => 3, 'label' => 'Contracts', 'children' => [
                ['id' => 4, 'label' => 'Signed'],
                ['id' => 5, 'label' => 'Drafts'],
            ]],
            ['id' => 6, 'label' => 'Reports'],
        ]],
        ['id' => 7, 'label' => 'Pictures', 'children' => [
            ['id' => 8, 'label' => 'Holidays'],
            ['id' => 9, 'label' => 'Family'],
        ]],
        ['id' => 10, 'label' => 'Archive (read only)', 'disabled' => true],
    ]);

Nodes ​

php
TreeView::make('site')->label('Site')->nodes([
    ['label' => 'Guide', 'href' => '/guide', 'expanded' => true, 'children' => [
        ['label' => 'Install', 'href' => '/guide/install'],
        ['label' => 'Data endpoints', 'href' => '/guide/data'],
    ]],
    ['label' => 'Components', 'href' => '/components'],
]);
KeyUse
labelThe node's text. Required.
idIts value in a selectable or checkable tree, and in a children endpoint's URL.
hrefMakes the node a link: relative, http or https only.
childrenIts child nodes.
expandedOpen at first. expandAll() opens every node.
checked (or selected)Checked, or selected, at first.
disabledCannot be selected or checked.
hasChildrenIts children come from url() when it first opens.

From flat rows with a parent field, e.g. categories from a database:

php
TreeView::make('categories')->label('Categories')
    ->fromRows($categories, id: 'id', parent: 'parent_id', label: 'name', href: '/categories/{id}');

A row whose parent is not among the rows is a root. Rows in a cycle (a row that is its own ancestor) have no root and are left out.

Selecting and checking ​

  • selectable('folder'): one node can be selected, a radio button named folder that posts its id.
  • checkable('folders'): nodes can be checked, checkboxes named folders[] that post the checked ids. Checking a parent checks its children; a parent with some of its children checked is mixed (aria-checked="mixed"), and does not post its own id.

The tree fires caelrik:change with {value}: the selected id, or the list of checked ids.

Children loaded on demand ​

php
$tree = TreeView::make('folders')->label('Folders')->url('/folders/{id}/children')
    ->nodes([['id' => 6, 'label' => 'Pictures', 'hasChildren' => true]]);

// GET /folders/{id}/children
echo json_encode(TreeView::children($folder->children()));   // {"data": [nodes]}

TreeView::children() checks the nodes as nodes() does. A node whose endpoint answers no children no longer opens. A failed load is announced, and opening the node again tries again.

Options ​

MethodUse
label($label)The tree's accessible name.
nodes(iterable $nodes), fromRows(...)The nodes.
selectable($name), checkable($name)Radio buttons or checkboxes, posted with the form.
url('/folders/{id}/children')Where hasChildren nodes load their children.
expandAll()Every node starts open.
labels(['loading' => ..., 'failed' => ...])Translates the announcements of a load.

Keyboard ​

KeyDoes
TabEnters and leaves the tree: it is one tab stop, on the selected or first checked node, or the first node.
Down, UpThe next or previous visible node.
RightOpens a node; on an open node, goes to its first child.
LeftCloses a node; on a closed node, goes to its parent.
Home, EndThe first or last visible node.
A letterThe next node starting with it.
*Opens the node's siblings.
EnterFollows the node's link, or selects or checks it, or opens or closes it.
SpaceSelects or checks the node.

Accessibility ​

  • The tree follows the WAI-ARIA tree pattern: each node's row is a treeitem, named by its own text, with its level, position, and open, selected or checked state. It owns its children's group (aria-owns).
  • Without JavaScript, the tree is nested lists: links work, and radio buttons and checkboxes post with the form.
  • The focused node shows an outline, also in Windows High Contrast; the selected node is drawn in the system's Highlight colors there.

Security ​

  • Labels are text: they are escaped, and loaded nodes are built with textContent.
  • Links other than relative, http or https are dropped, on the server and in the browser.

Released under the MIT License.