# Table

Source: https://ecosphere.dev/docs/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 base table: a header, hairline-separated rows, figure columns, sorting, selection, a sticky header and frozen columns.

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

## Table

Keep the identifying column first with a subtitle; put figures in a numeric column. Never stripe.

Vue (vue-ecosphere):

```vue
<EcoTable :columns="[
    { key: 'request', label: 'Request' },
    { key: 'owner', label: 'Owner' },
    { key: 'status', label: 'Status' },
    { key: 'age', label: 'Age', numeric: true, format: (v) => v + 'd' },
  ]" :rows="requests">
  <template #cell-request="{ row }">
    <span class="table__t">{{ row.request }}</span><span class="table__s">{{ row.sub }}</span>
  </template>
  <template #cell-owner="{ row }">
    <span class="table__cell"><span class="avatar avatar--xs" aria-hidden="true">{{ row.initials }}</span>{{ row.owner }}</span>
  </template>
  <template #cell-status="{ row }">
    <span class="badge" :class="'badge--' + row.tone"><span class="badge__dot" aria-hidden="true"></span>{{ row.status }}</span>
  </template>
</EcoTable>
```

React (react-ecosphere):

```tsx
<EcoTable
  columns={[
    { key: "request", label: "Request" },
    { key: "owner", label: "Owner" },
    { key: "status", label: "Status" },
    { key: "age", label: "Age", numeric: true, format: (v) => v + "d" },
  ]}
  rows={requests}
  renderCell={{
    request: ({ row }) => (
      <><span className="table__t">{row.request}</span><span className="table__s">{row.sub}</span></>
    ),
    owner: ({ row }) => (
      <span className="table__cell"><span className="avatar avatar--xs" aria-hidden="true">{row.initials}</span>{row.owner}</span>
    ),
    status: ({ row }) => (
      <span className={"badge badge--" + row.tone}><span className="badge__dot" aria-hidden="true"></span>{row.status}</span>
    ),
  }}
/>
```

## Compact

For dense review work, offered as the reader’s choice.

Vue (vue-ecosphere):

```vue
<EcoTable compact :columns="columns" :rows="requests"> … </EcoTable>
```

React (react-ecosphere):

```tsx
<EcoTable compact columns={columns} rows={requests} renderCell={…} />
```

## Sortable

Click a header: ascending, descending, then off. Only the sorted column shows its arrow.

Vue (vue-ecosphere):

```vue
<!-- sortable: true on a column; the table sorts its rows unless manual-sort is set -->
<EcoTable v-model:sort="sort" :columns="[
  { key: 'request', label: 'Request', sortable: true },
  { key: 'owner', label: 'Owner', sortable: true },
  { key: 'status', label: 'Status' },
  { key: 'age', label: 'Age', numeric: true, sortable: true, format: (v) => v + 'd' },
]" :rows="requests" />
```

React (react-ecosphere):

```tsx
// sortable: true on a column; the table sorts its rows unless manualSort is set
const [sort, setSort] = useState<EcoTableSort | null>(null);

<EcoTable
  sort={sort}
  onSortChange={setSort}
  columns={[
    { key: "request", label: "Request", sortable: true },
    { key: "owner", label: "Owner", sortable: true },
    { key: "status", label: "Status" },
    { key: "age", label: "Age", numeric: true, sortable: true, format: (v) => v + "d" },
  ]}
  rows={requests}
/>
```

## Selectable

Check rows, or the header to select the page. With some rows checked the header box is indeterminate.

Vue (vue-ecosphere):

```vue
<EcoTable selectable v-model:selected="selected" :columns="columns" :rows="requests" />
<!-- selected: ["r1", "r3"] — the header checkbox goes indeterminate for a partial page -->
```

React (react-ecosphere):

```tsx
const [selected, setSelected] = useState<EcoRowKey[]>(["r1", "r3"]);

<EcoTable selectable selected={selected} onSelectedChange={setSelected} columns={columns} rows={requests} />
// selected: ["r1", "r3"] — the header checkbox goes indeterminate for a partial page
```

## Sticky header

Whenever the body scrolls, keep the header: a header that scrolls away turns every figure into a guess.

Vue (vue-ecosphere):

```vue
<EcoTable sticky style="max-height: 260px" :columns="columns" :rows="many" />
```

React (react-ecosphere):

```tsx
<EcoTable sticky style={{ maxHeight: 260 }} columns={columns} rows={many} />
```

## Frozen first and last columns

A wide table scrolls sideways with the row’s name frozen at the left and its actions at the right. Never pin a middle column.

Vue (vue-ecosphere):

```vue
<EcoTable pin-first pin-last row-header :columns="[
    { key: 'request', label: 'Request' },
    { key: 'owner', label: 'Owner' }, { key: 'team', label: 'Team' }, { key: 'status', label: 'Status' },
    { key: 'age', label: 'Age', numeric: true }, { key: 'seats', label: 'Seats', numeric: true },
    { key: 'update', label: 'Last update' }, { key: 'region', label: 'Region' },
    { key: 'actions', label: 'Actions', action: true },
  ]" :rows="requests">
  <template #cell-actions="{ row }">
    <span class="table__act">
      <button class="iconbtn iconbtn--sm" type="button" :aria-label="'Open ' + row.request"><i class="ri-arrow-right-up-line" aria-hidden="true"></i></button>
      <button class="iconbtn iconbtn--sm" type="button" :aria-label="'More actions for ' + row.request" aria-haspopup="menu" aria-expanded="false"><i class="ri-more-2-fill" aria-hidden="true"></i></button>
    </span>
  </template>
</EcoTable>
```

React (react-ecosphere):

```tsx
<EcoTable
  pinFirst
  pinLast
  rowHeader
  columns={[
    { key: "request", label: "Request" },
    { key: "owner", label: "Owner" }, { key: "team", label: "Team" }, { key: "status", label: "Status" },
    { key: "age", label: "Age", numeric: true }, { key: "seats", label: "Seats", numeric: true },
    { key: "update", label: "Last update" }, { key: "region", label: "Region" },
    { key: "actions", label: "Actions", action: true },
  ]}
  rows={requests}
  renderCell={{
    actions: ({ row }) => (
      <span className="table__act">
        <button className="iconbtn iconbtn--sm" type="button" aria-label={"Open " + row.request}><i className="ri-arrow-right-up-line" aria-hidden="true"></i></button>
        <button className="iconbtn iconbtn--sm" type="button" aria-label={"More actions for " + row.request} aria-haspopup="menu" aria-expanded="false"><i className="ri-more-2-fill" aria-hidden="true"></i></button>
      </span>
    ),
  }}
/>
```

## Empty

With no rows, the #empty slot fills a full-width row. Say what will fill it.

Vue (vue-ecosphere):

```vue
<EcoTable :columns="columns" :rows="[]">
  <template #empty><p class="caption">No requests yet. New requests appear here as soon as someone files one.</p></template>
</EcoTable>
```

React (react-ecosphere):

```tsx
<EcoTable
  columns={columns}
  rows={[]}
  empty={<p className="caption">No requests yet. New requests appear here as soon as someone files one.</p>}
/>
```

## API

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `columns` | `EcoTableColumn[]` | — | { key, label, numeric?, width?, sortable?, action?, srLabel?, sortValue?, format? }. numeric renders col-num, right-aligned and tabular. |
| `rows` | `object[]` | — | Row objects; identity from rowKey. |
| `rowKey` | `string \| (row) => key` | "id" | Row identity for selection and keys. |
| `#cell-<key> / #head-<key>` | `{ row, value, index } / { column }` | — | Override a cell or a header. |
| `compact` | `boolean` | false | Dense rows, for scanning for one row among many. |
| `sticky` | `boolean` | false | Header stays put while the body scrolls. |
| `nowrap / fixed` | `boolean` | false | One line per row (table--nowrap); fixed layout (table--fixedw). |
| `pinFirst / pinLast` | `boolean` | false | Freeze the identifying first column and the action column. Only when the table scrolls. |
| `rowHeader` | `boolean` | false | First column as th scope="row". |
| `selectable` | `boolean` | false | Checkbox column with a select-all header (indeterminate for a partial page). |
| `v-model:selected` | `(string \| number)[]` | [] | Selected row keys. Selected rows take the violet tint and accent rule. |
| `v-model:sort` | `{ key, dir: "asc" \| "desc" } \| null` | null | Header click cycles ascending → descending → off; aria-sort marks the sorted column. |
| `manualSort` | `boolean` | false | Leave rows in the given order (server-side sorting); still emits update:sort. |
| `loading` | `boolean` | false | Skeleton rows and aria-busy. |
| `caption` | `string` | — | Visually hidden caption. |
| `#empty` | `—` | — | Shown in a full-width row when there are no rows. Say how rows will arrive. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `columns` | `EcoTableColumn[]` | — | { key, label, numeric?, width?, sortable?, action?, srLabel?, sortValue?, format? }. numeric renders col-num, right-aligned and tabular. |
| `rows` | `object[]` | — | Row objects; identity from rowKey. |
| `rowKey` | `string \| (row) => key` | "id" | Row identity for selection and keys. |
| `renderCell / renderHead` | `Record<string, (scope: { row, value, index }) => ReactNode> / Record<string, (scope: { column }) => ReactNode>` | — | Override a cell or a header. |
| `compact` | `boolean` | false | Dense rows, for scanning for one row among many. |
| `sticky` | `boolean` | false | Header stays put while the body scrolls. |
| `nowrap / fixed` | `boolean` | false | One line per row (table--nowrap); fixed layout (table--fixedw). |
| `pinFirst / pinLast` | `boolean` | false | Freeze the identifying first column and the action column. Only when the table scrolls. |
| `rowHeader` | `boolean` | false | First column as th scope="row". |
| `selectable` | `boolean` | false | Checkbox column with a select-all header (indeterminate for a partial page). |
| `selected / onSelectedChange` | `(string \| number)[]` | [] | Selected row keys. Selected rows take the violet tint and accent rule. |
| `sort / onSortChange` | `{ key, dir: "asc" \| "desc" } \| null` | null | Header click cycles ascending → descending → off; aria-sort marks the sorted column. |
| `manualSort` | `boolean` | false | Leave rows in the given order (server-side sorting); still calls onSortChange. |
| `loading` | `boolean` | false | Skeleton rows and aria-busy. |
| `caption` | `string` | — | Visually hidden caption. |
| `empty` | `ReactNode` | — | Shown in a full-width row when there are no rows. Say how rows will arrive. |
