render() table()
Render the table to HTML (or JSON); the table options passed to table() or here decide paging, layout and the empty case.
table()->render($params = [])Options
| Option | Meaning |
|---|---|
no_pages / pages_on_top / pages_on_bottom | No pager; the pager above the table; below it (the default). |
pager_records_on_page | Rows per page; default conf user_per_page / admin_per_page. |
pager_type, pager_size, pager_align, pager_path, pager_stpl_path, pager_add_get_vars | Pager of common()->divide_pages(): type, Bootstrap size (sm, lg), alignment, base path, template, keep the GET variables. |
extra | Extra options of the pager; ['url_path' => ...] sets the link base of array data. |
filter, filter_params, filter_name | Filter values (or true: the admin session filter of filter_name) and field => condition (eq, like, between, in...). |
condensed / very_condensed | Smaller rows (table-condensed under Bootstrap 3, table-sm under 5). |
table_class, table_class_add, force_class, table_attr | Classes after the default CLASS_TABLE_MAIN, added to it, replacing it; extra attributes of <table>. |
thead, tbody, tfoot | Attributes of those elements (string or [class, id] array). |
tr, td | Row / cell attributes: a string, an array by row id, or a callback fn ($row, $id) returning a string or ['class' => ...]. |
tr_variant | Callback fn ($row, $id) returning a contextual colour (success, danger, warning, info, active) for the row. |
responsive | true wraps the table in table-responsive; under Bootstrap 5 a breakpoint (md) too. |
sticky_header | true keeps the header in view; a height (400px, 60vh) makes a scroll box it sticks in. |
caption, show_total | Table caption; append the record count to it. |
no_header, no_total | No <thead>; no Total label in the admin. |
totals | Field => value or callback fn ($table, $info, $params, $a): a <tfoot> row of totals. |
group_by | Field(s) whose equal neighbours are merged with rowspan. |
hide_empty | Hide columns that are empty in every row. |
rotate_table | Fields as rows and records as columns (10 per page). |
actions_desc | Header of the buttons column; default Actions. |
buttons_group / buttons_dropdown_after | Row buttons in one btn-group; the first N buttons stay, the rest go into a menu. |
no_records_html, no_records_simple, no_records_callback | What an empty table shows instead of the No records alert. |
custom_fields | Name => [SQL with %ids, field] or callback: extra values per row, loaded once for all ids. |
auto, htmx, as_json, post_buttons | See auto(), htmx(); JSON of the data, pages and links; row buttons POST with the CSRF token. |
on_post, on_before_render, on_after_render | Callbacks, the same as on_post(). |
db | Database object for an SQL $data; default db(). |
Example
| City | Shop | Sales |
|---|---|---|
| Kyiv | Podil | 120 |
| Obolon | 95 | |
| Lviv | Rynok | 40 | 255 |
Nothing sold yet.
Code
$rows = [
1 => ['id' => 1, 'city' => 'Kyiv', 'shop' => 'Podil', 'sales' => 120],
2 => ['id' => 2, 'city' => 'Kyiv', 'shop' => 'Obolon', 'sales' => 95],
3 => ['id' => 3, 'city' => 'Lviv', 'shop' => 'Rynok', 'sales' => 40],
];
$table = table($rows, [
'condensed' => 1,
'caption' => 'Sales by shop',
'group_by' => 'city',
'tr_variant' => fn ($row) => $row['sales'] < 50 ? 'warning' : '',
'totals' => ['sales' => array_sum(array_column($rows, 'sales'))],
'no_pages' => 1,
])
->text('city')
->text('shop')
->text('sales');
$empty = table([], ['no_records_html' => '<p class="text-muted">Nothing sold yet.</p>'])->text('shop');
return $table->render(['responsive' => true]) . $empty;Notes
- Options given to
render()are merged over those oftable(); printing the table callsrender()without options. - With
as_jsonthe result is{data, pages, total, header_links, footer_links}as JSON. - Events
table.before_renderandtable.after_renderfire oncore_eventsfor every table. - The filter conditions quote and escape their values (
SAFE_FILTER_QUOTES, on);betweenandinlists are escaped too withESCAPE_FILTER_VALUES(off until 4.0).
Bootstrap 3 and 5
condensed is table-condensed (Bootstrap 3) or table-sm (5); tr_variant gives warning or table-warning; sticky_header is sticky-top under 5 and an inline sticky style under 3.
See also: table/table, table/column_options, demo:table/products, demo:table/bs5_components, demo:table/rotate