# Data table

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

The table with its working parts in one frame: title and count, tools, search, sortable headers, selection with a bulk bar, frozen columns and a pager.

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

## Toolbar, search, selection and frozen columns

Search, sort, check rows to see the selection bar, and delete them. Scroll sideways: the name and action columns stay.

Vue (vue-ecosphere):

```vue
<EcoDataTable
  title="Open requests"
  :count="rows.length"
  :columns="columns"
  :rows="shown"
  selectable
  pin-first
  pin-last
  max-height="330px"
  v-model:selected="selected"
  v-model:sort="sort"
  :row-label="(r) => r.request"
>
  <template #tools>
    <button class="iconbtn iconbtn--sm" type="button" aria-label="Refresh"><i class="ri-refresh-line" aria-hidden="true"></i></button>
    <button class="iconbtn iconbtn--sm" type="button" aria-label="Download"><i class="ri-download-2-line" aria-hidden="true"></i></button>
  </template>
  <template #actions="{ selected }">
    <button class="btn btn--secondary btn--sm" type="button"><i class="ri-user-shared-line" aria-hidden="true"></i>Reassign</button>
    <button class="btn btn--danger btn--sm" type="button" @click="remove(selected)">
      <i class="ri-delete-bin-line" aria-hidden="true"></i>Delete {{ selected.length }} requests</button>
  </template>
  <template #bar>
    <div class="dtable__find"><!-- EcoSearchField + "Clear filters" --></div>
  </template>
  <template #cell-request="{ row }">
    <span class="table__t">{{ row.request }}</span><span class="table__s">{{ row.sub }}</span>
  </template>
  <template #empty>
    <div class="empty">…No matches…</div>
  </template>
  <template #footer>
    <div class="pager pager--bar"><p class="pager__c">1–7 of 7</p>…</div>
  </template>
</EcoDataTable>
```

React (react-ecosphere):

```tsx
const [selected, setSelected] = useState<EcoRowKey[]>([]);
const [sort, setSort] = useState<EcoTableSort | null>({ key: "request", dir: "asc" });

<EcoDataTable
  title="Open requests"
  count={rows.length}
  columns={columns}
  rows={shown}
  selectable
  pinFirst
  pinLast
  maxHeight="330px"
  selected={selected}
  onSelectedChange={setSelected}
  sort={sort}
  onSortChange={setSort}
  rowLabel={(r) => r.request}
  tools={
    <>
      <button className="iconbtn iconbtn--sm" type="button" aria-label="Refresh"><i className="ri-refresh-line" aria-hidden="true"></i></button>
      <button className="iconbtn iconbtn--sm" type="button" aria-label="Download"><i className="ri-download-2-line" aria-hidden="true"></i></button>
    </>
  }
  renderActions={({ selected }) => (
    <>
      <button className="btn btn--secondary btn--sm" type="button"><i className="ri-user-shared-line" aria-hidden="true"></i>Reassign</button>
      <button className="btn btn--danger btn--sm" type="button" onClick={() => remove(selected)}>
        <i className="ri-delete-bin-line" aria-hidden="true"></i>Delete {selected.length} requests</button>
    </>
  )}
  bar={<div className="dtable__find">{/* EcoSearchField + "Clear filters" */}</div>}
  renderCell={{
    request: ({ row }) => (
      <><span className="table__t">{row.request}</span><span className="table__s">{row.sub}</span></>
    ),
  }}
  empty={<div className="empty">…No matches…</div>}
  footer={<div className="pager pager--bar"><p className="pager__c">1–7 of 7</p>…</div>}
/>
```

## Expandable rows

Open a row with its chevron to see the rest of its record. Rows still select, sort and pin as usual; the detail row stays under its row.

Vue (vue-ecosphere):

```vue
<EcoDataTable
  title="Open requests"
  :columns="columns"
  :rows="rows"
  selectable
  expandable
  compact
  v-model:selected="selected"
  v-model:expanded="expanded"
  :row-label="(r) => r.request"
>
  <template #cell-request="{ row }">
    <span class="table__t">{{ row.request }}</span><span class="table__s">{{ row.sub }}</span>
  </template>
  <template #expanded="{ row }">
    <EcoKeyValue grid :items="[
      { term: 'Destination', value: row.destination },
      { term: 'Region', value: row.region, num: true },
      { term: 'Created', value: row.created },
      { term: 'Seats', value: row.seats, num: true },
      { term: 'Monthly cost', value: money.format(row.cost), num: true },
    ]" />
  </template>
</EcoDataTable>
<!-- Only some rows: :expandable="(row) => row.status !== 'Blocked'" -->
```

React (react-ecosphere):

```tsx
const [selected, setSelected] = useState<EcoRowKey[]>([]);
const [expanded, setExpanded] = useState<EcoRowKey[]>([]);

<EcoDataTable
  title="Open requests"
  columns={columns}
  rows={rows}
  selectable
  expandable
  compact
  selected={selected}
  onSelectedChange={setSelected}
  expanded={expanded}
  onExpandedChange={setExpanded}
  rowLabel={(r) => r.request}
  renderCell={{
    request: ({ row }) => (
      <><span className="table__t">{row.request}</span><span className="table__s">{row.sub}</span></>
    ),
  }}
  renderExpanded={({ row }) => (
    <EcoKeyValue grid items={[
      { term: "Destination", value: row.destination },
      { term: "Region", value: row.region, num: true },
      { term: "Created", value: row.created },
      { term: "Seats", value: row.seats, num: true },
      { term: "Monthly cost", value: money.format(row.cost), num: true },
    ]} />
  )}
/>
// Only some rows: expandable={(row) => row.status !== "Blocked"}
```

## Loading and error

Keep the header while a page loads. Never show an empty table for an error: say what happened and offer Retry.

Vue (vue-ecosphere):

```vue
<EcoDataTable title="Open requests" :columns="columns" :rows="[]" loading />
<EcoDataTable title="Open requests" error="The server took too long to sort by Monthly cost. Clearing the sort usually helps." @retry="load">
  <template #error-actions>
    <button class="btn btn--secondary btn--sm" type="button" @click="sort = null">Clear sorting</button>
    <button class="btn btn--secondary btn--sm" type="button" @click="load">Retry</button>
  </template>
</EcoDataTable>
```

React (react-ecosphere):

```tsx
<EcoDataTable title="Open requests" columns={columns} rows={[]} loading />
<EcoDataTable
  title="Open requests"
  error="The server took too long to sort by Monthly cost. Clearing the sort usually helps."
  onRetry={load}
  errorActions={
    <>
      <button className="btn btn--secondary btn--sm" type="button" onClick={() => setSort(null)}>Clear sorting</button>
      <button className="btn btn--secondary btn--sm" type="button" onClick={load}>Retry</button>
    </>
  }
/>
```

## API

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `title / count` | `string / string \| number` | — | dtable__t in the display face, and the total beside it. |
| `label` | `string` | — | aria-label of the frame (defaults to title). |
| `columns / rows / rowKey` | `—` | — | Passed to EcoTable; #cell-<key> and #head-<key> slots are forwarded. |
| `selectable + v-model:selected` | `(string \| number)[]` | — | When rows are checked the top bar switches to is-sel: “N selected”, #actions, and Clear selection. |
| `v-model:sort / manualSort` | `EcoTableSort \| null / boolean` | — | Sorting, internal unless manualSort (server-side). |
| `sticky / nowrap / rowHeader` | `boolean` | true | The frame’s defaults: sticky header, one line per row, first column as row header. |
| `pinFirst / pinLast / compact` | `boolean` | false | Freeze the name (and selection) column and the action column; compact rows. |
| `maxHeight` | `string` | — | Height of dtable__scroll (stylesheet default 420px). is-scrolled is set while scrolled sideways. |
| `loading` | `boolean` | false | Header stays, skeleton rows in the body, aria-busy. |
| `error / errorTitle` | `string` | — / "Rows did not load." | A notice with Retry (@retry) instead of the table. #error-actions overrides the buttons. |
| `#tools / #bar / #footer` | `—` | — | Toolbar end, search and filter bar (dtable__bar), and pager (dtable__foot). |
| `#actions` | `{ selected, clear }` | — | Bulk actions in the selection bar. One filled button; a destructive one names the count. |
| `#empty` | `—` | — | EmptyState markup in the frame when there are no rows. |
| `expandable + v-model:expanded` | `boolean \| (row) => boolean / (string \| number)[]` | false / [] | Rows that open a full-width detail row (#expanded) under them, and the keys of the open ones. Adds a chevron toggle column after the selection column; frozen columns stay aligned. |
| `#expanded` | `{ row, index }` | — | Content of an open row's detail row: a row's secondary facts, not child rows (use EcoTreeTable for a hierarchy). |
| `expandLabel / collapseLabel` | `string \| (row) => string` | "Show details for" / "Hide details for" | The toggle's name: a prefix before rowLabel(row), or the whole name from the row (for word orders a prefix cannot give). |
| `expandColumnLabel` | `string` | "Details" | Visually hidden header of the toggle column. |
| `@expand / @collapse` | `(row)` | — | A row's detail row opened or closed. |
| `default slot` | `—` | — | Frame any other table (e.g. EcoTreeTable) in dtable__scroll instead. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `title / count` | `string / string \| number` | — | dtable__t in the display face, and the total beside it. |
| `label` | `string` | — | aria-label of the frame (defaults to title). |
| `columns / rows / rowKey` | `—` | — | Passed to EcoTable, with renderCell and renderHead. |
| `selectable + selected / onSelectedChange` | `(string \| number)[]` | — | When rows are checked the top bar switches to is-sel: “N selected”, renderActions, and Clear selection. |
| `sort / onSortChange / manualSort` | `EcoTableSort \| null / boolean` | — | Sorting, internal unless manualSort (server-side). |
| `sticky / nowrap / rowHeader` | `boolean` | true | The frame’s defaults: sticky header, one line per row, first column as row header. |
| `pinFirst / pinLast / compact` | `boolean` | false | Freeze the name (and selection) column and the action column; compact rows. |
| `maxHeight` | `string` | — | Height of dtable__scroll (stylesheet default 420px). is-scrolled is set while scrolled sideways. |
| `loading` | `boolean` | false | Header stays, skeleton rows in the body, aria-busy. |
| `error / errorTitle` | `string` | — / "Rows did not load." | A notice with Retry (onRetry) instead of the table. errorActions (ReactNode) replaces the buttons. |
| `tools / bar / footer` | `ReactNode` | — | Toolbar end, search and filter bar (dtable__bar), and pager (dtable__foot). |
| `renderActions` | `(scope: { selected, clear }) => ReactNode` | — | Bulk actions in the selection bar. One filled button; a destructive one names the count. |
| `empty` | `ReactNode` | — | EmptyState markup in the frame when there are no rows. |
| `expandable + expanded / onExpandedChange` | `boolean \| (row) => boolean / (string \| number)[]` | false / [] | Rows that open a full-width detail row (renderExpanded) under them, and the keys of the open ones. Adds a chevron toggle column after the selection column; frozen columns stay aligned. |
| `renderExpanded` | `(scope: { row, index }) => ReactNode` | — | Content of an open row's detail row: a row's secondary facts, not child rows (use EcoTreeTable for a hierarchy). |
| `expandLabel / collapseLabel` | `string \| (row) => string` | "Show details for" / "Hide details for" | The toggle's name: a prefix before rowLabel(row), or the whole name from the row (for word orders a prefix cannot give). |
| `expandColumnLabel` | `string` | "Details" | Visually hidden header of the toggle column. |
| `onExpand / onCollapse` | `(row)` | — | A row's detail row opened or closed. |
| `children` | `ReactNode` | — | Frame any other table (e.g. EcoTreeTable) in dtable__scroll instead. |
