on_post(), on_before_render(), on_after_render() table()
Callbacks around rendering: on a POST before the rows are drawn, before rendering with the data, after rendering with the HTML.
table()->on_post($func)
table()->on_before_render($func)
table()->on_after_render($func)Options
| Option | Meaning |
|---|---|
on_post | fn ($params, $a, $table): $a holds data, ids, pages, total. Runs only when the request is a POST. |
on_before_render | fn ($params, $data, $table): change $table (add columns) once the data is loaded. |
on_after_render | fn ($params, $a, $body, $table): sees the finished HTML (logging, counters). |
Example
| Name | Price |
|---|---|
| Desk lamp | 24.9 |
| Office chair | 149 |
before render: 2 rows after render: 224 bytes of HTML
Code
$rows = [
1 => ['id' => 1, 'name' => 'Desk lamp', 'price' => 24.9],
2 => ['id' => 2, 'name' => 'Office chair', 'price' => 149],
];
$log = [];
$table = table($rows, ['no_pages' => 1])
->text('name')
->on_before_render(function ($params, $data, $table) use (&$log) {
$log[] = 'before render: ' . count($data) . ' rows';
$table->text('price');
})
->on_after_render(function ($params, $a, $body) use (&$log) {
$log[] = 'after render: ' . strlen($body) . ' bytes of HTML';
})
->on_post(function ($params, $a) use (&$log) {
$log[] = 'posted';
});
$html = $table->render();
return $html . '<pre>' . _prepare_html(implode(PHP_EOL, $log)) . '</pre>';Notes
- Several callbacks of one kind run in order. The table options
on_post,on_before_renderandon_after_renderreplace those registered with the methods. - A POST handler checks the CSRF token and the rights before it changes anything.
See also: table/render, form/events