# Data Table

- **Section:** Components
- **URL:** https://refast.fastapicloud.dev/docs/components/data-table
- **Markdown:** https://refast.fastapicloud.dev/docs/components/data-table.md
- **Source:** https://github.com/idling-mind/refast-docs/blob/main/docs/pages/components/data_display/data_table.py
- **Live examples:** the widgets on this page need a browser; open the URL above to try them.
- **Applies to:** refast 0.12.2

High-level data table with built-in sorting, filtering, and pagination.
Pass column definitions and row data — the component handles all UI concerns.
For full layout control use the primitive [Table](https://refast.fastapicloud.dev/docs/components/table) family instead.

```python
from refast.components import DataTable

DataTable(
    columns=[
        {"key": "name",   "header": "Name",   "sortable": True},
        {"key": "email",  "header": "Email",  "sortable": True},
        {"key": "role",   "header": "Role",   "align": "center"},
    ],
    data=[
        {"name": "Alice", "email": "alice@example.com", "role": "Admin"},
        {"name": "Bob",   "email": "bob@example.com",   "role": "Editor"},
    ],
    sortable=True,
    filterable=True,
    paginated=True,
    page_size=10,
)
```

## Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `columns` | `list[dict]` | *(required)* | Column definitions. Each dict needs `key` and `header`. |
| `data` | `list[dict]` | *(required)* | Row data. Each dict should have all column keys. |
| `sortable` | `bool` | `True` | Enable clicking column headers to sort. |
| `filterable` | `bool` | `True` | Show a text filter input above the table. |
| `paginated` | `bool` | `True` | Show pagination controls below the table. |
| `page_size` | `int` | `10` | Number of rows per page. |
| `loading` | `bool` | `False` | Show a loading overlay. |
| `empty_message` | `str` | `"No data available"` | Text shown when no rows match. |
| `current_page` | `int \| None` | `None` | Controlled current page (1-based). |
| `on_row_click` | `Callback \| None` | `None` | Fired when a row is clicked. `ctx.event_data` is the row dict. |
| `on_sort_change` | `Callback \| None` | `None` | Fired on sort. `ctx.event_data` is `{"key": ..., "direction": "asc"/"desc"}` or `None`. |
| `on_filter_change` | `Callback \| None` | `None` | Fired on filter input change. `ctx.event_data` is `{"value": ...}`. |
| `on_page_change` | `Callback \| None` | `None` | Fired on page change. `ctx.event_data` is `{"page": N}`. |
| `class_name` | `str` | `""` | Extra Tailwind classes. |

## Column definition keys

| Key | Required | Description |
|-----|----------|-------------|
| `key` | ✓ | Field name used to look up values in each row. |
| `header` | ✓ | Column header label. |
| `sortable` | | Per-column sortable override. |
| `width` | | CSS width (e.g. `"200px"`, `"20%"`). |
| `align` | | `"left"` (default), `"center"`, `"right"`. |

## Server-side pagination

```python
async def on_page(ctx: Context):
    page = ctx.event_data.get("page", 1)
    ctx.state.set("page", page)
    await ctx.refresh()

def render(ctx: Context):
    page = ctx.state.get("page", 1)
    page_data = fetch_page(page, size=10)
    return DataTable(
        columns=[...],
        data=page_data,
        paginated=True,
        page_size=10,
        current_page=page,
        on_page_change=ctx.callback(on_page),
    )
```

---

Part of the Refast documentation: [all pages](https://refast.fastapicloud.dev/llms.txt), [everything on one page](https://refast.fastapicloud.dev/llms-full.txt), [search](https://refast.fastapicloud.dev/api/search?q=save_prop).
