DataTables.net
Searches, sorts and pages a server-rendered table in the browser with the datatables.net (3.x) library.
zb:datatables zubazar/ui-pro
Needs a JavaScript library registration: see Setup.
This component is in Zubazar UI Pro; Zubazar UI Pro brings Zubazar UI with it. Installing Zubazar UI Pro →
Basic usage
Put a plain table with the ui-table class inside zb:datatables and pass DataTables options with options.
<zb:datatables class="rounded-sm bg-surface shadow-sm" :options="['order' => [[0, 'desc']], 'pageLength' => 5]">
<table class="ui-table">
<thead><tr><th>Order no</th><th>Customer</th><th>Product</th><th>Status</th><th class="text-end">Amount</th></tr></thead>
<tbody>
@foreach ($orders as $order)
<tr><td class="font-medium text-ink">{{ $order->number }}</td><td>{{ $order->customer_name }}</td><td>{{ $order->product }}</td><td><zb:badge :variant="$order->status->variant()">{{ $order->status->label() }}</zb:badge></td><td class="text-end tabular-nums" data-order="{{ $order->amount }}">{{ $order->formattedAmount() }}</td></tr>
@endforeach
</tbody>
</table>
</zb:datatables>
| Prop | Values |
|---|---|
options | DataTables options (order, pageLength, columnDefs…); the page-size select is off, a table that wants it sets lengthChange: true |
data-order | The cell's sort value (a formatted amount, a date) |
On a Livewire page
A formatted amount or date sorts correctly with data-order, and a cell with several pieces of data is searched with data-search.
<zb:card title="Recent orders" :padding="false">
<zb:datatables :options="['order' => [[2, 'desc']], 'pageLength' => 25, 'language' => ['searchPlaceholder' => 'Order, customer, product…']]">
<table class="ui-table">
<thead>
<tr><th>Order no</th><th>Customer</th><th>Date</th><th>Status</th><th class="text-end">Amount</th></tr>
</thead>
<tbody>
@foreach ($orders as $order)
<tr>
<td class="font-medium text-ink">{{ $order->number }}</td>
<td data-search="{{ $order->customer_name }} {{ $order->customer_email }}">{{ $order->customer_name }}</td>
<td data-order="{{ $order->ordered_at->timestamp }}">{{ $order->ordered_at->translatedFormat('M j, Y') }}</td>
<td><zb:badge :variant="$order->status->variant()">{{ $order->status->label() }}</zb:badge></td>
<td class="text-end tabular-nums" data-order="{{ $order->amount }}">{{ $order->formattedAmount() }}</td>
</tr>
@endforeach
</tbody>
</table>
</zb:datatables>
</zb:card>
| Prop | Values |
|---|---|
data-search | The cell's search text |
language | Changes one text, the rest stays in the page's language |
About
Every row comes with the page, so it suits up to a few thousand records. For large lists filtered on the server, use the kit's list parts with your own Livewire component and Laravel pagination (the List parts page and the List page guide).
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
options | — | [] | DataTables options: order, pageLength, columnDefs, searching, paging, language…; lengthChange: true (and lengthMenu) for a page-size select |
options.language | — | The page's language | Turkish texts on a Turkish page, DataTables' English elsewhere; the keys you give are laid over them |
slot | — | — | The table with the `ui-table` class; the first `<table>` or `x-ref="table"` |
registerDataTables: language | — | The page's language | The texts every table uses |
Installation
Install DataTables and register it in resources/js/app.js.
// npm install datatables.net
// resources/js/app.js
document.addEventListener('livewire:init', () => {
const { lazyLibrary, registerDataTables } = window.ZubazarUI.pro;
registerDataTables(window.Alpine, lazyLibrary(() => import('datatables.net')));
});
Developer notes
- No jQuery is needed; the search box, page-size select, sort arrows and paging take the theme's colours and follow the light and dark theme.
- The look is in Zubazar UI Pro's CSS; DataTables' own stylesheet is not needed.
lazyLibrarydownloads the library only on a page with a table.- On a Turkish page (
<html lang="tr">) the texts are Turkish on their own; texts given withregisterDataTables(Alpine, DataTable, language)are used in every table. - The table is read once (
wire:ignore); to rebuild it with new rows, give it awire:keythat changes with the rows. - **Security:** rows given through
options(ajax,data,columns) are drawn as HTML by DataTables. Escape the users' text on the server or give the columnrender: DataTable.render.text(); a table the server draws (Blade{{ }}) is safe already.