htmx() table()
Sort headers, a live filter and a pager or "load more" row that htmx swaps without reloading the page.
table()->htmx($options = [])Options
| Option | Meaning |
|---|---|
id | Id of the swap box ([a-z][a-z0-9_-]*); default yf-table2-1, -2... per request. |
sort | true (default): text / link / date / stars columns; a list of keys or key => column; false: no sort headers. |
filter | true or a list of filter_params keys: a search field each above the table, sent while typing. |
pager | pages (default), more (a "Load more" row) or infinite (it loads when scrolled into view). |
delay | Debounce of the live filter in ms; default 300. |
tiebreak | Unique column appended to the ORDER BY, so pages keep one order. |
push_url | hx-push-url on headers, pager and filter; default true. |
url | Base of the links (site code, a path from the root or an http(s) URL); default the request URI. |
sort_param, dir_param, filter_param | Query parameter names; default sort, dir, f. |
filter_max_length | Longest live filter value; default 100. |
Example
Code
$rows = [];
foreach (['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry', 'Fig', 'Grape', 'Kiwi', 'Lemon', 'Mango', 'Orange', 'Peach'] as $i => $name) {
$rows[$i + 1] = ['id' => $i + 1, 'name' => $name, 'price' => number_format(0.5 + $i * 0.35, 2)];
}
return table($rows, ['pager_records_on_page' => 4, 'filter_params' => ['name' => 'like']])
->htmx(['id' => 'ref-fruits', 'sort' => ['name', 'price'], 'filter' => ['name']])
->text('name')
->text('price');Notes
- The same as the table option
htmx;htmx(false)turns it off. Without the option the table prints what it printed before, byte for byte. - For an htmx request that targets the box,
render()answers with the box alone through_class('htmx')->_result()and ends the request. - Sorting is whitelisted:
?sort=is looked up in the keys ofsortonly, the ORDER BY is built from the column name in backticks. - Without JavaScript the headers and the pager are links and the filter a GET form.
Bootstrap 3 and 5
Filter fields are input-sm / btn-default under Bootstrap 3 and form-control-sm / btn-outline-secondary under 5; the hidden labels use sr-only or visually-hidden.
See also: htmx/table2, htmx/fragment, demo:table/htmx, demo:htmx/table