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

OptionMeaning
no_pages / pages_on_top / pages_on_bottomNo pager; the pager above the table; below it (the default).
pager_records_on_pageRows per page; default conf user_per_page / admin_per_page.
pager_type, pager_size, pager_align, pager_path, pager_stpl_path, pager_add_get_varsPager of common()->divide_pages(): type, Bootstrap size (sm, lg), alignment, base path, template, keep the GET variables.
extraExtra options of the pager; ['url_path' => ...] sets the link base of array data.
filter, filter_params, filter_nameFilter values (or true: the admin session filter of filter_name) and field => condition (eq, like, between, in...).
condensed / very_condensedSmaller rows (table-condensed under Bootstrap 3, table-sm under 5).
table_class, table_class_add, force_class, table_attrClasses after the default CLASS_TABLE_MAIN, added to it, replacing it; extra attributes of <table>.
thead, tbody, tfootAttributes of those elements (string or [class, id] array).
tr, tdRow / cell attributes: a string, an array by row id, or a callback fn ($row, $id) returning a string or ['class' => ...].
tr_variantCallback fn ($row, $id) returning a contextual colour (success, danger, warning, info, active) for the row.
responsivetrue wraps the table in table-responsive; under Bootstrap 5 a breakpoint (md) too.
sticky_headertrue keeps the header in view; a height (400px, 60vh) makes a scroll box it sticks in.
caption, show_totalTable caption; append the record count to it.
no_header, no_totalNo <thead>; no Total label in the admin.
totalsField => value or callback fn ($table, $info, $params, $a): a <tfoot> row of totals.
group_byField(s) whose equal neighbours are merged with rowspan.
hide_emptyHide columns that are empty in every row.
rotate_tableFields as rows and records as columns (10 per page).
actions_descHeader of the buttons column; default Actions.
buttons_group / buttons_dropdown_afterRow buttons in one btn-group; the first N buttons stay, the rest go into a menu.
no_records_html, no_records_simple, no_records_callbackWhat an empty table shows instead of the No records alert.
custom_fieldsName => [SQL with %ids, field] or callback: extra values per row, loaded once for all ids.
auto, htmx, as_json, post_buttonsSee auto(), htmx(); JSON of the data, pages and links; row buttons POST with the CSRF token.
on_post, on_before_render, on_after_renderCallbacks, the same as on_post().
dbDatabase object for an SQL $data; default db().

Example

City Shop Sales
Kyiv Podil 120
Obolon 95
Lviv Rynok 40
255
Sales by shop

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 of table(); printing the table calls render() without options.
  • With as_json the result is {data, pages, total, header_links, footer_links} as JSON.
  • Events table.before_render and table.after_render fire on core_events for every table.
  • The filter conditions quote and escape their values (SAFE_FILTER_QUOTES, on); between and in lists are escaped too with ESCAPE_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