Grid
A data table on DataTables 3: sorting, search, column filters, column totals and row groups, inline editing, columns the user can show or hide, row actions, row selection with bulk actions, paging and export, client-side or server-side.
<?php
use Caelrik\Ui\Components\Action;
use Caelrik\Ui\Components\Grid;
use Caelrik\Ui\Components\Grid\Column;
use Caelrik\Ui\Components\Grid\Filter;
return Grid::make('users')
->caption('Team members')
->column('name', 'Name', static fn (Column $column) => $column->filter(Filter::text())->toggleable(false))
->column('role', 'Role', static fn (Column $column) => $column->filter(Filter::select([
'Engineer' => 'Engineer',
'Researcher' => 'Researcher',
'Director' => 'Director',
])))
->column('remote', 'Remote', static fn (Column $column) => $column->filter(Filter::boolean()))
->column('joined', 'Joined', static fn (Column $column) => $column->filter(Filter::dateRange()))
->actions(
Action::link('profile', 'Profile', '?member={name}'),
// The demo answers requests in the browser: nothing is saved.
Action::request('remove', 'Remove', '/demo-api/members/{name}')
->method('DELETE')
->variant('danger')
->confirm('Remove {name}?', 'They lose access to the team.'),
)
->columnToggle()
->emptyState('No members yet', 'Invite your team to get started.', Action::link('invite', 'Invite', '?invite=1'))
->selectable('name', 'members')
->bulkActions(
Action::request('remind', 'Send a reminder', '/demo-api/members/remind')
->confirm('Send a reminder to {count} members?'),
)
->export('copy', 'csv')
->pageSize(5)
->rows([
['name' => 'Ada Lovelace', 'role' => 'Engineer', 'remote' => true, 'joined' => '2024-03-12'],
['name' => 'Alan Turing', 'role' => 'Researcher', 'remote' => false, 'joined' => '2023-11-02'],
['name' => 'Grace Hopper', 'role' => 'Director', 'remote' => false, 'joined' => '2022-06-20'],
['name' => 'Katherine Johnson', 'role' => 'Researcher', 'remote' => true, 'joined' => '2025-01-15'],
['name' => 'Linus Torvalds', 'role' => 'Engineer', 'remote' => true, 'joined' => '2024-09-30'],
['name' => 'Margaret Hamilton', 'role' => 'Director', 'remote' => false, 'joined' => '2021-04-08'],
['name' => 'Tim Berners-Lee', 'role' => 'Engineer', 'remote' => true, 'joined' => '2025-07-01'],
]);Client-side or server-side
- Client-side: pass the rows with
rows(). PHP renders them in the table, and the browser sorts, searches and pages them. Good for up to a few hundred rows. - Server-side: pass an endpoint with
url(), and answer it withrespond()using the same grid definition (see Data endpoints). The browser asks for one page at a time.
$grid = Grid::make('users')->caption('Users')->url('/users/data')->column('name')->column('email', 'E-mail');
// GET /users/data
echo json_encode($grid->respond(new ArrayDataSource($rows), $_GET));Options
| Method | Use |
|---|---|
column($field, $title = null, $configure = null) | Adds a column; the title defaults to the field name. $configure receives the Column. |
addColumn(Column ...$columns) | Adds prepared columns (Column::make('email', 'E-mail')). |
caption($caption, $visuallyHidden = false) | The table's caption, which names it for screen readers. Always set one. |
rows(iterable $rows) | Client-side data: arrays or objects with the column fields (dot notation for nested values). |
url($url) | Server-side mode. |
pageSize($size), pageSizes([10, 25, 50]) | Rows per page, and the choices offered. |
searchable(false) | Hides the search box. |
export('copy', 'csv', 'print') | Export buttons. |
actions(Action ...$actions) | Buttons for each row, in a last column (see Row actions). |
inlineEdit($url, $method = 'PATCH'), csrf($token) | Where cell changes are saved, and the CSRF token sent with them (see Inline editing). |
groupBy($field) | Groups the rows by a column, with a heading row per group (see Totals and groups). |
emptyState($title, $description = null, $action = null) | What the grid shows when it has no rows (see Empty states). |
columnToggle($enabled = true, $remember = true) | A Columns menu to show or hide columns (see Columns the user can show or hide). |
selectable($key = 'id', $name = 'ids') | A checkbox per row (see Row selection). |
bulkActions(Action ...$actions) | Request actions for the selected rows, in a bar above the table. |
rowTitle($field) | The field that names a row in its buttons' and checkbox's accessible names. Default: the first visible column. |
labels([...]) | Translates the filter bar, the actions column and the selection: filters, any, yes, no, from, to, clear, actions, select, selectAll, selected (with {count}), noneSelected, bulkActions, columns, showColumns, empty, noMatches, reset, the totals' sum, avg, count, min, max, the groups' groupRow and groupRows (with {count}) and noGroup, the number separators decimal and thousands, and inline editing's edit and editField (with {column} and {row}), saved, editFailed, required, invalid, tooLong, tooLow and tooHigh. |
options([...]) | Raw DataTables options, applied last. |
Columns
Grid::make('orders')
->column('id', 'ID', static fn (Column $column) => $column->searchable(false)->width('5rem'))
->column('customer.name', 'Customer')
->column('total', 'Total', static fn (Column $column) => $column->className('text-end'))
->column('internal_ref', null, static fn (Column $column) => $column->hidden());| Method | Use |
|---|---|
sortable(false) | Not sortable (also refused by respond()). |
searchable(false) | Not searched (also refused as a filter). |
hidden() | Sent to the client but not shown, e.g. an id for row actions. Never in the Columns menu. |
toggleable(false) | Always shown: not in the Columns menu. |
initiallyHidden() | Hidden until the user shows it from the Columns menu. |
className($class), width($width) | CSS class of the cells, and the column width. |
filter(Filter::text()) | A filter in the filter bar (see Column filters). |
editable(Editor::text()) | The cell can be changed in place (see Inline editing). |
total($function = 'sum', $decimals = null) | A total in the footer: sum, avg, count, min or max (see Totals and groups). |
Column filters
A column with a filter gets a labelled field in a filter bar above the table, with a Clear filters button. The rows update as the user types or picks a value.
use Caelrik\Ui\Components\Grid\Column;
use Caelrik\Ui\Components\Grid\Filter;
Grid::make('orders')
->column('customer', 'Customer', static fn (Column $c) => $c->filter(Filter::text()))
->column('status', 'Status', static fn (Column $c) => $c->filter(Filter::select([
'open' => 'Open',
'paid' => 'Paid',
'refunded' => 'Refunded',
])))
->column('gift', 'Gift', static fn (Column $c) => $c->filter(Filter::boolean()))
->column('ordered_at', 'Ordered', static fn (Column $c) => $c->filter(Filter::dateRange()));| Filter | Field | Keeps the rows whose value |
|---|---|---|
Filter::text() | A search box | contains the text, ignoring case |
Filter::select([value => label]) | A select, with Any first | is the chosen value |
Filter::boolean() | A select: Any, Yes, No | is true or false (1 or 0 in a database) |
Filter::dateRange() | Two date fields, From and To, grouped under the column title | is a date (Y-m-d, with a time or not) in the range, both days included |
- Client-side, the browser filters the rows PHP rendered.
- Server-side, the grid sends the filters to your endpoint, and
respond()applies them through the data source:ArrayDataSource,EloquentDataSourceandDoctrineDataSourceall do (see Data endpoints). - Filters and search work together, and a filter works on a column that is not
searchable(). Withsearchable(false)on the grid, the search box is hidden and the filters keep working. - Without JavaScript, the filter bar is not shown, since it needs it to filter.
Totals and groups
<?php
use Caelrik\Ui\Components\Grid;
use Caelrik\Ui\Components\Grid\Column;
return Grid::make('expenses')
->caption('Expenses by team')
->column('team', 'Team', static fn (Column $column) => $column->hidden())
->column('item', 'Item')
->column('date', 'Date')
->column('amount', 'Amount', static fn (Column $column) => $column->total()->className('text-end'))
->column('nights', 'Nights', static fn (Column $column) => $column->total('avg', 1)->className('text-end'))
->groupBy('team')
->pageSize(10)
->rows([
['team' => 'Sales', 'item' => 'Flights', 'date' => '2026-10-02', 'amount' => 1240.5, 'nights' => 4],
['team' => 'Design', 'item' => 'Font licences', 'date' => '2026-10-05', 'amount' => 380, 'nights' => null],
['team' => 'Sales', 'item' => 'Hotel', 'date' => '2026-10-06', 'amount' => 860, 'nights' => 4],
['team' => 'Engineering', 'item' => 'Tickets', 'date' => '2026-10-09', 'amount' => 1500, 'nights' => 2],
['team' => 'Design', 'item' => 'Usability study', 'date' => '2026-10-12', 'amount' => 2200, 'nights' => 1],
['team' => 'Engineering', 'item' => 'Laptops', 'date' => '2026-10-15', 'amount' => 4800, 'nights' => null],
['team' => 'Sales', 'item' => 'Client dinner', 'date' => '2026-10-20', 'amount' => 145.75, 'nights' => null],
]);A column with total() gets a total in the table's footer, and groupBy() groups the rows:
Grid::make('expenses')
->column('team', 'Team', static fn (Column $c) => $c->hidden())
->column('item', 'Item')
->column('amount', 'Amount', static fn (Column $c) => $c->total()) // sum
->column('nights', 'Nights', static fn (Column $c) => $c->total('avg', 1)) // 1 decimal
->groupBy('team');- Totals cover every row that passes the search and filters, not only the page, and follow them as they change. Empty values are left out, as in SQL:
countcounts the others, andsum,avg,minandmaxonly read numbers. - Numbers have no decimals for a count or a whole number, and 2 otherwise, unless you give
$decimals.labels(['decimal' => ',', 'thousands' => ' '])sets the separators. - Groups are sorted first, whatever the user sorts by: sorting a column sorts the rows within each group. Each group on the page starts with a heading row that names it, says how many rows it has and gives its totals. Its count and totals cover the whole group, also when it goes on to the next page.
- The group column can be
hidden(): its value is in the heading row. - Server-side,
respond()asks the data source for the totals and for the groups of the page, and the endpoint's answer carries them (see Data endpoints). The built-in sources compute them in the database. - Without JavaScript, the footer has the totals of all rows, and the rows are in group order.
- For screen readers, a group's heading is a header cell for the rows that follow it (
<th scope="rowgroup">), and the footer's totals sit under their column headers.
Inline editing
<?php
use Caelrik\Ui\Components\Grid;
use Caelrik\Ui\Components\Grid\Column;
use Caelrik\Ui\Components\Grid\Editor;
// The demo answers edits in the browser: nothing is saved.
return Grid::make('crew')
->caption('Crew')
->column('id', 'ID', static fn (Column $column) => $column->hidden())
->column('name', 'Name', static fn (Column $column) => $column->editable(Editor::text(60)->required()))
->column('role', 'Role', static fn (Column $column) => $column->editable(Editor::select([
'engineer' => 'Engineer',
'researcher' => 'Researcher',
'director' => 'Director',
])->required()))
->column('remote', 'Remote', static fn (Column $column) => $column->editable(Editor::boolean()))
->column('hours', 'Hours a week', static fn (Column $column) => $column
->editable(Editor::number(0, 60))
->total())
->column('since', 'Since', static fn (Column $column) => $column->editable(Editor::date()))
->inlineEdit('/demo-api/crew/{id}')
->pageSize(5)
->rows([
[
'id' => 1, 'name' => 'Ada Lovelace', 'role' => 'engineer', 'remote' => true, 'hours' => 32,
'since' => '2024-03-12',
],
['id' => 2, 'name' => 'Alan Turing', 'role' => 'researcher', 'remote' => false, 'hours' => 40, 'since' => null],
['id' => 3, 'name' => 'Grace Hopper', 'role' => 'director', 'remote' => false, 'hours' => 38, 'since' => null],
['id' => 4, 'name' => 'Mary Jackson', 'role' => 'researcher', 'remote' => true, 'hours' => 24, 'since' => null],
]);A column with an Editor can be changed in place, and inlineEdit() says where changes are saved:
$grid = Grid::make('crew')
->column('id', 'ID', static fn (Column $c) => $c->hidden())
->column('name', 'Name', static fn (Column $c) => $c->editable(Editor::text(60)->required()))
->column('role', 'Role', static fn (Column $c) => $c->editable(Editor::select(['eng' => 'Engineer', 'dir' => 'Director'])))
->inlineEdit('/crew/{id}')
->csrf($token);
// POST /crew/{id}
try {
$edit = $grid->cellEdit($_POST); // only an editable column, with a value its editor allows
$crew->update($id, [$edit->field => $edit->value]);
echo json_encode($edit->answer()); // {"value": ...}
} catch (InvalidCellEdit $refusal) {
http_response_code(422);
echo json_encode($refusal->toArray()); // {"message": ..., "errors": {"name": [...]}}
}| Editor | Field | Value cellEdit() gives |
|---|---|---|
Editor::text($maxLength = null) | A text box | The trimmed text |
Editor::number($min = null, $max = null, $step = null) | A number box | An int or a float |
Editor::select(['value' => 'Label']) | A list | One of the values; the cell shows its label |
Editor::boolean() | Yes or No | true or false |
Editor::date() | A date picker | A day, Y-m-d |
->required() refuses an empty value; otherwise an empty value is null.
- From the keyboard: each editable cell has an edit button, named after its column and its row ("Edit Role of Ada Lovelace"). It opens a field named the same way. Enter saves, Escape cancels, and leaving the field saves. Focus then goes back to the button. A double click on the cell opens the field too.
- Checked twice: the browser checks the value with the editor's rules before sending it, and
cellEdit()checks it again. A refused value keeps the field open, with the message as its description (aria-describedby). A refusal from your endpoint is shown the same way:errors[field], ormessage. - The request is a form-encoded
POSTwithfield,value,_method(PATCHby default) and the CSRF field, plus the CSRF header. PHP reads it in$_POST, and Laravel and Symfony read_method.{id}in the URL takes the row's value, so add a column for it (it can behidden()). - The answer:
{"value": ...}sets the cell to the value as saved, e.g. rounded. Amessageis shown in a toast; without one, "Role of Ada Lovelace saved." is announced. The cell firescaelrik:editwith{field, value, ok, status}. - Totals of a client-side grid follow the change. A server-side grid's totals come with its next page of rows.
- Without JavaScript, cells show their values and no edit button.
Empty states
A grid without rows says so, and helps the user to move on:
Grid::make('members')
->emptyState(
'No members yet',
'Invite your team to get started.',
Action::link('invite', 'Invite', '/members/invite')->variant('primary')->solid(),
);- With no rows at all, the grid shows the title, the description and the action. The default is "No rows to show".
- When the search or filters match no row, the grid says so, with a Clear filters and search button. The button clears both, and focus moves to the search box, or the first filter.
- Both work client-side and server-side, where the endpoint's
totaltells them apart.labels()translatesempty,noMatchesandreset.
Columns the user can show or hide
columnToggle() adds a Columns button next to the page length. It opens a group of checkboxes, one for each column the user may show or hide.
Grid::make('contacts')
->column('id', 'ID', static fn (Column $c) => $c->hidden()) // data only: not in the menu
->column('name', 'Name', static fn (Column $c) => $c->toggleable(false)) // always shown
->column('email', 'E-mail')
->column('phone', 'Phone', static fn (Column $c) => $c->initiallyHidden())
->columnToggle();- The choice is remembered in the browser (
localStorage), per grid id.columnToggle(remember: false)turns that off. - The last shown column cannot be hidden, unless a
toggleable(false)column is always there. - The menu is a disclosure: the button says whether it is open (
aria-expanded), and Escape closes it and returns focus to the button. It also closes when focus or a click leaves it. - Without JavaScript, every column but
hidden()ones is shown,initiallyHidden()ones included.
Row actions
actions() adds a last column with a button for each row: actions that open a page or send a request.
use Caelrik\Ui\Components\Action;
Grid::make('members')
->url('/members/data')
->column('id', 'ID', static fn (Column $c) => $c->hidden())
->column('name', 'Name')
->column('email', 'E-mail')
->actions(
Action::link('edit', 'Edit', '/members/{id}/edit'),
Action::request('delete', 'Delete', '/members/{id}')
->method('DELETE')
->variant('danger')
->confirm('Delete {name}?', 'This cannot be undone.')
->csrf($csrfToken),
);{field}takes the row's value. The field must be a column of the grid, hidden if it should not show: in server-side moderespond()returns nothing else. The grid throws aLogicExceptionnaming the missing column.Each button is named with its label and the row's title, for example "Delete Ada Lovelace", because a screen reader lists many "Delete" buttons otherwise.
rowTitle()chooses the field, andariaLabel()on an action replaces the name.After a request succeeds:
- a server-side grid reloads its page of rows;
- a client-side grid removes the row of a
DELETE.
Focus moves to the same button in the row that takes its place, or to the table when the page is empty.
Exports leave the actions column out.
Row selection and bulk actions
selectable() adds a first column with a checkbox for each row, and a box in its header that selects every row of the page. bulkActions() adds a bar above the table: the number of rows selected, and a button for each action.
Grid::make('members')
->url('/members/data')
->column('id', 'ID', static fn (Column $c) => $c->hidden())
->column('name', 'Name')
->selectable('id')
->bulkActions(
Action::request('delete', 'Delete selected', '/members/bulk-delete')
->method('DELETE')
->variant('danger')
->confirm('Delete {count} members?', 'This cannot be undone.')
->csrf($csrfToken),
);
// DELETE /members/bulk-delete: ids[]=4&ids[]=9
// Check authorization, and that the user may change each of these rows.
$ids = array_map('intval', $_POST['ids'] ?? []);The key (
idby default) identifies a row. It must be a column of the grid, hidden or not.The selection is kept across pages, filters and sorting.
- The header box is checked when every row of the page is selected, and mixed when some are.
- Every checkbox is named after its row, for example "Select Ada Lovelace".
The bar says how many rows are selected, in a status that screen readers announce. Its buttons are disabled while no row is selected.
A bulk action posts the selected keys as
ids[](the second argument ofselectable()names them), with the action's method and the CSRF token.{count}in its confirmation is the number of rows.After a bulk action succeeds:
- a server-side grid reloads its page;
- a client-side grid removes the rows of a
DELETE.
The selection is then cleared, and focus moves to the header box.
Without JavaScript, the checkboxes of a client-side grid belong to the bulk form, so a bulk action posts the checked rows of the page.
Exports leave the selection column out.
Accessibility
- A real
<table>with a<caption>andscope="col"headers, before and after JavaScript. - Sort buttons are named ("Name: Activate to sort") and
aria-sortreports the order; DataTables announces results in a polite status region. - Every control can be used from the keyboard, and focus stays visible in Windows High Contrast.
- The filter bar is a
searchlandmark named "Filters". Each field has a visible label (the column title); a date range is a group named by the column title, with From and To fields. Paging information is updated as rows are filtered.
Security
respond() only sorts, searches and filters declared columns, rejects malformed field names, escapes % and _ in the search term and text filters, and returns only declared fields, whatever your data source returns. A column filter applies only as its type says: a yes/no filter accepts 1 or 0, a date range only real dates written Y-m-d, and anything else is ignored. Cells are always rendered as text.