v0.0.2
Demos (demo panel, opens in a new tab)

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

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

Basic usage: view
BLADE

Put a plain table with the ui-table class inside zb:datatables and pass DataTables options with options.

blade
<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>
PropValues
optionsDataTables options (order, pageLength, columnDefs…); the page-size select is off, a table that wants it sets lengthChange: true
data-orderThe cell's sort value (a formatted amount, a date)

On a Livewire page

BLADE

A formatted amount or date sorts correctly with data-order, and a cell with several pieces of data is searched with data-search.

blade
<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>
PropValues
data-searchThe cell's search text
languageChanges 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?

PropTypeDefaultDescription
options—[]DataTables options: order, pageLength, columnDefs, searching, paging, language…; lengthChange: true (and lengthMenu) for a page-size select
options.language—The page's languageTurkish 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 languageThe texts every table uses

Installation

Install DataTables and register it in resources/js/app.js.

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

Events, hooks and details
  • 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.
  • lazyLibrary downloads 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 with registerDataTables(Alpine, DataTable, language) are used in every table.
  • The table is read once (wire:ignore); to rebuild it with new rows, give it a wire:key that 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 column render: DataTable.render.text(); a table the server draws (Blade {{ }}) is safe already.
Zubazar UI · ZubazarDesigned for people.