# Tree table

Source: https://ecosphere.dev/docs/data/tree-table (Ecosphere 1.5). Install `vue-ecosphere` for Vue 3 or `react-ecosphere` for React 18.2+/19: same components, props, markup and stylesheet.

Nested rows inside a table: a row with children gets a toggle, and opening it shows that level’s rows beneath it, indented one step per level.

`import { EcoTreeTable, EcoDataTable } from "vue-ecosphere"`

## Nested rows

Open York (it loads), Hull (it fails once; Retry) or South region. “Show 2 more” appends to North region, including a level that comes back empty.

Vue (vue-ecosphere):

```vue
<EcoDataTable title="Organisation units" count="3 regions">
  <EcoTreeTable
    v-model:expanded="expanded"
    caption="Organisation units. A row with a toggle lists its child units directly below it."
    :columns="columns"
    :rows="rows"
    @toggle="(row, open) => open && loadLevel(row)"
    @retry="loadLevel"
    @more="appendPage"
  >
    <template #cell-parent="{ value }"><a class="link" href="#">{{ value }}</a></template>
    <template #cell-status="{ value }">
      <span class="badge" :class="value === 'Open' ? 'badge--accent' : 'badge--neutral'">
        <span class="badge__dot" aria-hidden="true"></span>{{ value }}</span>
    </template>
  </EcoTreeTable>
</EcoDataTable>

// rows: { id, cells: { unit, kind, … }, href?, sub?, children?, hasChildren?,
//         state?: "loading" | "error" | "empty", stateText?, more?: { message, label } }
```

React (react-ecosphere):

```tsx
const [expanded, setExpanded] = useState(["north", "leeds"]);

<EcoDataTable title="Organisation units" count="3 regions">
  <EcoTreeTable
    expanded={expanded}
    onExpandedChange={setExpanded}
    caption="Organisation units. A row with a toggle lists its child units directly below it."
    columns={columns}
    rows={rows}
    onToggle={(row, open) => { if (open) loadLevel(row); }}
    onRetry={loadLevel}
    onMore={appendPage}
    renderCell={{
      parent: ({ value }) => <a className="link" href="#">{String(value)}</a>,
      status: ({ value }) => (
        <span className={value === "Open" ? "badge badge--accent" : "badge badge--neutral"}>
          <span className="badge__dot" aria-hidden="true"></span>{String(value)}</span>
      ),
    }}
  />
</EcoDataTable>

// rows: { id, cells: { unit, kind, … }, href?, sub?, children?, hasChildren?,
//         state?: "loading" | "error" | "empty", stateText?, more?: { message, label } }
```

## A repeated unit

A row whose id is already among its ancestors never gets a toggle, so a cycle cannot loop requests.

## API

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `columns` | `EcoTableColumn[]` | — | The first column is the name cell (th scope=row) with the toggle or spacer. |
| `rows` | `EcoTreeTableRow[]` | — | { id, cells, href?, sub?, children?, hasChildren?, state?, stateText?, more? }. Rows stay flat tr in one tbody. |
| `v-model:expanded` | `string[]` | [] | Open rows. Keep them in the URL with the rest of the table’s query. |
| `caption` | `string` | — | Say that a row with a toggle lists its child units directly below it. |
| `compact / sticky / nowrap` | `boolean` | false / false / true | Table modifiers. |
| `repeatText` | `string` | — | Second line on a row that repeats among its ancestors (no toggle, loop icon). |
| `#cell-<key>` | `{ row, value, depth }` | — | Override a cell. |
| `#more` | `{ row }` | — | Replace the “Show more” button, e.g. with a pager--compact. |
| `@toggle / @retry / @more` | `(row[, open]) => void` | — | Load a level when it opens, reload a failed level, append the next page. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `columns` | `EcoTableColumn[]` | — | The first column is the name cell (th scope=row) with the toggle or spacer. |
| `rows` | `EcoTreeTableRow[]` | — | { id, cells, href?, sub?, children?, hasChildren?, state?, stateText?, more? }. Rows stay flat tr in one tbody. |
| `expanded / onExpandedChange` | `string[]` | [] | Open rows. Keep them in the URL with the rest of the table’s query. |
| `caption` | `string` | — | Say that a row with a toggle lists its child units directly below it. |
| `compact / sticky / nowrap` | `boolean` | false / false / true | Table modifiers. |
| `repeatText` | `string` | — | Second line on a row that repeats among its ancestors (no toggle, loop icon). |
| `renderCell` | `Record<string, (scope: { row, value, depth }) => ReactNode>` | — | Override a cell. |
| `renderMore` | `(scope: { row }) => ReactNode` | — | Replace the “Show more” button, e.g. with a pager--compact. |
| `onToggle / onRetry / onMore` | `(row[, open]) => void` | — | Load a level when it opens, reload a failed level, append the next page. |
