# Table

> Shows records in rows and columns, with the shared datatable layout and pagination.

A table shows records in rows and columns; every Sosialoka product lays out its datatables the same way.

## When to use

- Use it for records people compare or scan, such as releases, documents or payouts.
- Use cards for a few rich items, and a plain list for one column of text.

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/table/_

### Do and don't

- **Do:** Right-align numbers with tabular figures, so the digits line up.
- **Don't:** Don't center numbers; their magnitudes become hard to compare.

## Anatomy

1. Header row on muted
2. Row
3. Status badge

The shared datatable has four parts:

1. **Header**: column names in `muted-foreground`, medium weight.
2. **Rows**: `border-b border-border` between rows, `bg-muted/50` on hover. Status uses a [Badge](https://design.sosialoka.tech/components/badge/).
3. **Row actions**: a ghost icon button that opens a [Dropdown Menu](https://design.sosialoka.tech/components/dropdown-menu/), in the last column.
4. **Pagination**: below the table, inside the same card. The current page is an `outline` button.

Put the table in a card (`rounded-lg border border-border bg-card`) so it reads as one surface.

## Variants

There is one style. Add `text-right` and `tabular-nums` to number columns.

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `Table`, `TableHeader`, `TableBody`, `TableRow`, `TableHead`, `TableCell` | same names | `ShadTable.list`, `ShadTableCell` | element | none | Table structure. |
| `PaginationLink isActive` | `PaginationItem :is-active` | `ShadButton.outline` | boolean | `false` | Marks the current page. |
| `href` on pagination links | `:total`, `:items-per-page` on `Pagination` | none | string / number | none | Where a page link goes, or how Vue counts pages. |

## Code

**React**
```tsx
import { useState, type MouseEvent } from "react"
import { Badge } from "@/components/ui/badge"
import {
  Pagination,
  PaginationContent,
  PaginationItem,
  PaginationLink,
  PaginationNext,
  PaginationPrevious,
} from "@/components/ui/pagination"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"

const releases = [
  { title: "Senja di Kota", artist: "Rara Senandika", status: "success", label: "Terbit" },
  { title: "Kaset Pertama", artist: "Bima Laras", status: "warning", label: "Menunggu" },
  { title: "Pulang Sore", artist: "Nada Kirana", status: "info", label: "Dijadwalkan" },
  { title: "Hujan Bulan Juni", artist: "Arka Wening", status: "success", label: "Terbit" },
  { title: "Lampu Kota", artist: "Sinta Maharani", status: "destructive", label: "Ditolak" },
] as const

/** Two pages of five rows; the demo shows the first page's rows on both. */
const pages = [1, 2]

export default function TableDemo() {
  const [page, setPage] = useState(1)
  const goTo = (next: number) => (event: MouseEvent) => {
    event.preventDefault()
    setPage(next)
  }
  return (
    <div className="w-full max-w-xl rounded-lg border border-border bg-card">
      <Table>
        <TableHeader>
          <TableRow>
            <TableHead>Judul</TableHead>
            <TableHead>Artis</TableHead>
            <TableHead>Status</TableHead>
          </TableRow>
        </TableHeader>
        <TableBody>
          {releases.map((r) => (
            <TableRow key={r.title}>
              <TableCell className="font-medium">{r.title}</TableCell>
              <TableCell className="text-muted-foreground">{r.artist}</TableCell>
              <TableCell>
                <Badge variant={r.status}>{r.label}</Badge>
              </TableCell>
            </TableRow>
          ))}
        </TableBody>
      </Table>
      <Pagination className="border-t border-border py-2">
        <PaginationContent>
          <PaginationItem>
            <PaginationPrevious href="#" disabled={page === 1} onClick={goTo(page - 1)} />
          </PaginationItem>
          {pages.map((p) => (
            <PaginationItem key={p}>
              <PaginationLink href="#" isActive={p === page} onClick={goTo(p)}>
                {p}
              </PaginationLink>
            </PaginationItem>
          ))}
          <PaginationItem>
            <PaginationNext href="#" disabled={page === pages.length} onClick={goTo(page + 1)} />
          </PaginationItem>
        </PaginationContent>
      </Pagination>
    </div>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { Badge } from "@/components/ui/badge"
import {
  Pagination,
  PaginationContent,
  PaginationItem,
  PaginationNext,
  PaginationPrevious,
} from "@/components/ui/pagination"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"

const releases = [
  { title: "Senja di Kota", artist: "Rara Senandika", status: "success", label: "Terbit" },
  { title: "Kaset Pertama", artist: "Bima Laras", status: "warning", label: "Menunggu" },
  { title: "Pulang Sore", artist: "Nada Kirana", status: "info", label: "Dijadwalkan" },
  { title: "Hujan Bulan Juni", artist: "Arka Wening", status: "success", label: "Terbit" },
  { title: "Lampu Kota", artist: "Sinta Maharani", status: "destructive", label: "Ditolak" },
] as const
</script>

<template>
  <div class="w-full max-w-xl rounded-lg border border-border bg-card">
    <Table>
      <TableHeader>
        <TableRow>
          <TableHead>Judul</TableHead>
          <TableHead>Artis</TableHead>
          <TableHead>Status</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        <TableRow v-for="r in releases" :key="r.title">
          <TableCell class="font-medium">{{ r.title }}</TableCell>
          <TableCell class="text-muted-foreground">{{ r.artist }}</TableCell>
          <TableCell><Badge :variant="r.status">{{ r.label }}</Badge></TableCell>
        </TableRow>
      </TableBody>
    </Table>
    <Pagination v-slot="{ page }" :items-per-page="5" :total="10" :default-page="1" class="border-t border-border py-2">
      <PaginationContent v-slot="{ items }">
        <PaginationPrevious />
        <template v-for="(item, index) in items" :key="index">
          <PaginationItem v-if="item.type === 'page'" :value="item.value" :is-active="item.value === page">
            {{ item.value }}
          </PaginationItem>
        </template>
        <PaginationNext />
      </PaginationContent>
    </Pagination>
  </div>
</template>
```

**Flutter**
```dart
// shadcn_ui has no pagination widget: put ShadButton.outline (current page) and ShadButton.ghost below the table.
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

const releases = [
  (title: 'Senja di Kota', artist: 'Rara Senandika', status: 'Terbit'),
  (title: 'Kaset Pertama', artist: 'Bima Laras', status: 'Menunggu'),
  (title: 'Pulang Sore', artist: 'Nada Kirana', status: 'Dijadwalkan'),
];

class TableDemo extends StatelessWidget {
  const TableDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: const BoxConstraints(maxWidth: 576, maxHeight: 320),
      child: ShadTable.list(
        // Fixed widths for the title and artist; the status column takes the rest, so badges never clip.
        columnSpanExtent: (index) => switch (index) {
          0 => const FixedTableSpanExtent(200),
          1 => const FixedTableSpanExtent(180),
          _ => const RemainingTableSpanExtent(),
        },
        header: const [
          ShadTableCell.header(child: Text('Judul')),
          ShadTableCell.header(child: Text('Artis')),
          ShadTableCell.header(child: Text('Status')),
        ],
        children: releases.map(
          (r) => [
            ShadTableCell(child: Text(r.title, style: const TextStyle(fontWeight: FontWeight.w500))),
            ShadTableCell(child: Text(r.artist)),
            ShadTableCell(child: ShadBadge.outline(child: Text(r.status))),
          ],
        ),
      ),
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/table.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/table.json
```

Pagination is a separate item:

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/pagination.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/pagination.json
```

## Accessibility

- Uses native `table`, `th` and `td`, so screen readers announce rows and columns.
- Pagination is a `nav`; the current page has `aria-current="page"`.
- Give icon-only row actions an `aria-label`, such as "Aksi".
