Abdulkader Safi

LaralCN-UI data table: sorting "$316.00" as 31600

LaralCN-UI now has a data table built from a columns array and a rows array, with filtering, sorting, row selection and pagination in about 100 lines of plain JavaScript. The interesting parts were keeping formatted cells sortable and a page label that read "Page 1 of 1s".

4 min read

Share
The LaralCN-UI data table showing payments sorted by amount, with two rows selected and the "2 of 8 row(s) selected" counter below.

LaralCN-UI now has a data-table component. You pass it columns and rows from the controller, and it renders a real <table> with an optional filter box, sortable headers, row checkboxes and client-side pagination. It shipped in PR #7 with the other components designed in Figma, and two details took more thought than the rest: sorting cells that display formatted text, and a page label that said "Page 1 of 1s".

Columns and rows, nothing else

shadcn's data table is a recipe around TanStack Table. That does not translate to Blade, so I started from what a Laravel controller already has: an array of rows and a list of fields to show. The simplest column is a key and a label. The longer form adds alignment, per-column sorting and a format callable:

<x-ui.data-table
    :columns="[
        'email' => 'Email',
        'amount' => ['label' => 'Amount', 'align' => 'end', 'format' => fn ($cents) => '$' . number_format($cents / 100, 2)],
    ]"
    :rows="$payments" sortable selectable searchable :per-page="10" />

Rows can be arrays, objects, Collections or a paginator. The component runs them through iterator_to_array($rows, false) and reads every cell with data_get($row, $key), so nested keys like customer.name work too.

Sorting what the user sees, by what it is

Sorting on the text in the cell does not survive format. Once 31600 cents becomes "$316.00", a string comparison puts "$1,299.00" before "$45.00". So the component keeps the two values apart on the server. The cell shows the formatted value, and the raw one goes into a data attribute:

$value = data_get($row, $key);
$display = $col['format'] ? ($col['format'])($value, $row) : $value;
$sortValue = is_bool($value) ? (int) $value : (is_scalar($value) ? $value : $display);

The script then compares numbers as numbers and everything else with localeCompare in numeric mode, so "item 10" sorts after "item 9":

function compare(a, b) {
    if (a !== '' && b !== '' && isFinite(a) && isFinite(b)) return Number(a) - Number(b);
    return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' });
}

Clicking a header cycles ascending, descending, then back to none. "None" restores the original order from each row's data-index, which is what you want after poking at a table to see who paid the most. The state lives in aria-sort on the <th>, and the arrow icons switch on that same attribute with group-aria-[sort=ascending]/sort:block, so screen readers and the visuals can never disagree.

Selection that ends up in the form

Each row checkbox is a real <input type="checkbox" name="selected[]"> with the row's id as its value. Put the table inside a <form> and the checked rows submit with no JavaScript on your side. select-name and row-key change the field name and the value column:

<form method="POST" action="/payments/bulk-refund">
    @csrf
    <x-ui.data-table :columns="$columns" :rows="$payments" selectable select-name="ids" row-key="id" />
    <x-ui.button type="submit">Refund selected</x-ui.button>
</form>

The header checkbox follows the filter. It selects every row that matches the current search, including rows on other pages, goes indeterminate when only some are checked, and disables itself when the filter matches nothing. After every change the root fires a bubbling data-table-select event with detail.selected, an array of the checked values, for anyone who wants a live "Delete 3 rows" button.

The page label bug

Pagination labels are props so they can be translated. The default is Page :page of :pages. My first replace call swapped :page first, which also matched the start of :pages and left the trailing "s" behind. On a one-page table the footer read "Page 1 of 1s". It surfaced in the browser check, because no test looks at the string after JavaScript runs. The fix is to replace the longer placeholder first, in both PHP and JS:

label.textContent = root.dataset.pageLabel.replace(':pages', pages).replace(':page', page);

One more pagination detail: the server already hides every row past the first page with the hidden attribute. Without that, a 200-row table renders in full and then snaps down to 10 rows when the script runs.

Where the client side stops

Everything in the script works on the rows that were rendered. That is fine for a few hundred rows. For a big table, paginate in the controller, pass $paginator->items() and render the pagination component under the table. Sorting then covers the current page only, so for server-wide sorting you would link the headers to a query string yourself.

Where it is

composer require --dev safi/laralcn-ui
php artisan ui:init
php artisan ui:add data-table

Docs and a live demo are at https://laralcn-ui.abdulkadersafi.com/components/data-table (Arabic at /ar/components/data-table). Source on GitHub: https://github.com/Abdulkader-Safi/LaralCN-UI. MIT licensed.

Next: the date picker, which gets its month names from the browser and still submits Y-m-d.

Last updated 1 Dec 2026 · filed under Laravel, blade, javascript, laralcn-ui

Every entry on this project

26 build notes, in order

Including the ones where nothing worked. You are on part 15.

Read the series →

FAQ

Frequently asked questions

Can I pass an Eloquent collection or a paginator straight in?

Yes. The component calls iterator_to_array on rows and reads each cell with data_get, so arrays, models, Collections and $paginator->items() all work.

How do I show a formatted price but still sort by the number?

Give the column a format callable. The cell shows its return value, and the raw value goes into data-sort-value, which is what the sort compares.

How do I get the selected rows on the server?

Wrap the table in a form. Checked rows submit as selected[] (or whatever select-name says), holding the row-key value of each row.

Does it sort across all pages of a server-side paginator?

No. Sorting and filtering only see the rows on the page that was rendered, so server-wide sorting needs your own query string handling.

Written by

Abdulkader Safi

Software Engineer

Lead engineer at DSRPT, from Lebanon and based in Kuwait. I write about the tools and bugs from real client work, with the numbers I measured.

About me → GitHub LinkedIn

Need this kind of work done on your project?

Start a project →

Keep reading

More from LaralCN-UI: shadcn-style Blade components and blocks you own

All 26 entries →