--- title: "data-table" description: "Table built from columns and rows with client-side filter, sortable headers, row selection and pagination." category: "Data Display" --- # data-table Table built from columns and rows with client-side filter, sortable headers, row selection and pagination. ## Installation ```bash php artisan ui:add data-table ``` ## Dependencies - composer: `gehrisandro/tailwind-merge-laravel` ## Usage ```blade {{-- Plain columns, nothing interactive --}} {{-- Every feature on. `format` changes what a cell shows; the raw value still sorts. --}} {{-- Inside a form the checked rows submit as ids[] --}}
@csrf Refund selected {{-- Large data: paginate on the server and render the pagination component below --}} ``` ## Example The demo rendered on the docs page. ```blade @php // In a real app these come from the controller. $columns = [ 'status' => 'Status', 'email' => 'Email', 'amount' => [ 'label' => 'Amount', 'align' => 'end', 'format' => fn ($cents) => '$' . number_format($cents / 100, 2), ], ]; $payments = [ ['id' => 'm5gr84i9', 'status' => 'Success', 'email' => 'ken99@example.com', 'amount' => 31600], ['id' => '3u1reuv4', 'status' => 'Success', 'email' => 'abe45@example.com', 'amount' => 24200], ['id' => 'derv1ws0', 'status' => 'Processing', 'email' => 'monserrat44@example.com', 'amount' => 83700], ['id' => '5kma53ae', 'status' => 'Success', 'email' => 'silas22@example.com', 'amount' => 87400], ['id' => 'bhqecj4p', 'status' => 'Failed', 'email' => 'carmella@example.com', 'amount' => 72100], ['id' => 'p0r8sd2k', 'status' => 'Pending', 'email' => 'lina.haddad@example.com', 'amount' => 4500], ['id' => 'x7c2lm9q', 'status' => 'Success', 'email' => 'omar.k@example.com', 'amount' => 129900], ['id' => 'h3n6tz1w', 'status' => 'Processing', 'email' => 'jules@example.com', 'amount' => 15000], ]; @endphp ``` ## Source Copy this file to the path shown above it. `resources/views/components/ui/data-table.blade.php` ```blade @props([ 'columns' => [], 'rows' => [], 'sortable' => false, 'selectable' => false, 'searchable' => false, 'perPage' => null, 'rowKey' => 'id', 'selectName' => 'selected', 'empty' => 'No results.', 'searchPlaceholder' => 'Filter...', 'searchLabel' => 'Filter rows', 'selectAllLabel' => 'Select all rows', 'selectRowLabel' => 'Select row', 'selectedLabel' => ':selected of :total row(s) selected.', 'previousLabel' => 'Previous', 'nextLabel' => 'Next', 'pageLabel' => 'Page :page of :pages', ]) @php // Columns come as ['email' => 'Email'] or ['amount' => ['label' => 'Amount', // 'align' => 'end', 'sortable' => true, 'format' => fn ($value, $row) => ...]]. // `format` changes what the cell shows; the raw value still drives sorting. $cols = []; foreach ($columns as $key => $column) { $column = is_array($column) ? $column : ['label' => $column]; $cols[$key] = [ 'label' => $column['label'] ?? $key, 'sortable' => $column['sortable'] ?? $sortable, 'format' => $column['format'] ?? null, 'align' => match ($column['align'] ?? 'start') { 'end' => 'text-end', 'center' => 'text-center', default => 'text-start', }, ]; } // Arrays, Collections and paginators all iterate the same way. $items = iterator_to_array($rows, false); $total = count($items); $perPage = $perPage ? max(1, (int) $perPage) : null; $pages = $perPage ? max(1, (int) ceil($total / $perPage)) : 1; $classes = \TailwindMerge\Laravel\Facades\TailwindMerge::merge( 'group/data-table flex w-full flex-col gap-4', $attributes->get('class'), ); $checkbox = 'size-4 shrink-0 rounded-[4px] border border-input bg-transparent align-middle accent-primary shadow-xs outline-none transition-shadow disabled:cursor-not-allowed disabled:opacity-50 dark:bg-input/30 focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]'; $pagerButton = 'inline-flex h-8 shrink-0 items-center justify-center gap-1.5 whitespace-nowrap rounded-md border border-input bg-background px-3 text-sm font-medium text-foreground shadow-xs outline-none transition-all hover:bg-accent hover:text-accent-foreground disabled:pointer-events-none disabled:opacity-50 dark:bg-input/30 dark:hover:bg-input/50 focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]'; @endphp
except('class')->merge(['class' => $classes]) }} data-ui-data-table data-page="1" data-per-page="{{ $perPage }}" data-selected-label="{{ $selectedLabel }}" data-page-label="{{ $pageLabel }}"> @if ($searchable) @endif
@if ($selectable) @endif @foreach ($cols as $key => $col) @if ($col['sortable']) @else @endif @endforeach @foreach ($items as $index => $row) = $perPage) hidden @endif class="border-t transition-colors hover:bg-muted/50 has-checked:bg-muted"> @if ($selectable) @endif @foreach ($cols as $key => $col) @php $value = data_get($row, $key); $display = $col['format'] ? ($col['format'])($value, $row) : $value; $sortValue = is_bool($value) ? (int) $value : (is_scalar($value) ? $value : $display); @endphp @endforeach @endforeach 0) hidden @endif>
{{ $col['label'] }}
{{ $display }}
{{ $empty }}
@if ($selectable || $perPage)

@if ($selectable) {{ str_replace([':selected', ':total'], [0, $total], $selectedLabel) }} @endif

@if ($perPage)
{{ str_replace([':pages', ':page'], [$pages, 1], $pageLabel) }}
@endif
@endif
@once @endonce ``` ## Theme tokens `--muted`, `--muted-foreground`, `--border`, `--input`, `--accent`, `--ring`, `--radius` Pass `columns` (`['email' => 'Email']` or `['amount' => ['label' => 'Amount', 'align' => 'end', 'format' => fn ($value, $row) => ...]]`) and `rows` (arrays or objects); `format` changes the display while the raw value drives sorting. `sortable`, `searchable`, `selectable` and `per-page` work on the rendered rows only, so pass a paginator's items and render `pagination` below for large data. Selected rows submit as `selected[]` (`select-name`, value from `row-key`), and the root fires `data-table-select` with `detail.selected`.