func() table()
A column whose cells your callback renders from the value and the whole row.
table()->func($name, $func = null, $extra = [])Options
| Option | Meaning |
|---|---|
$func | Callback fn ($value, $info, $row, $params, $table) returning the cell HTML; $info['extra'] holds the options. |
name | With a callback as the first argument: the field name; default func_1, func_2... (see the notes before using that form). |
(common options) | The common column options. |
Example
| Name | Price | Total |
|---|---|---|
| Desk lamp | $24.90 | 3 × $24.90 = $74.70 |
| Office chair | $149.00 | 1 × $149.00 = $149.00 |
Code
$rows = [
1 => ['id' => 1, 'name' => 'Desk lamp', 'price' => 24.9, 'qty' => 3],
2 => ['id' => 2, 'name' => 'Office chair', 'price' => 149, 'qty' => 1],
];
return table($rows, ['no_pages' => 1])
->text('name')
->func('price', fn ($value) => '$' . number_format($value, 2))
->func('qty', fn ($value, $info, $row) => $value . ' × $' . number_format($row['price'], 2) . ' = <b>$' . number_format($row['price'] * $value, 2) . '</b>', ['desc' => 'Total']);Notes
- The callback returns HTML: escape values that come from users with
_prepare_html(). - Give the callback a field the rows have, as above. With a callback as the first argument (no field) the callback is by default also registered as a
custom_fieldsloader: it is first called as($ids_sql, $db_func, $table, $data, $ids)and a truthy answer is run as SQL (known bug, also behind the SQL error of rownum()).yf_table2::$FUNC_NOT_DATA_LOADER = true($PROJECT_CONF['table2']['FUNC_NOT_DATA_LOADER'], Settings / Widget fixes; the 4.0 default) fixes it: the callback then only renders, with anullvalue. Without the flag do not use that form.
See also: table/column_options, table/btn_func, form/func