tr(), td() table()

Register row and cell callbacks that add attributes; applied only with the TR_TD_CALLBACKS flag, otherwise use the tr and td table options.

table()->tr($func, $extra = [])
table()->td($func, $extra = [])

Options

OptionMeaning
$funcCallback; tr() gets ($row, $id), td() gets ($row, $field, $row_id) ($field is buttons for the button cell), as the options. Returns an attribute string or ['class' => ..., 'style' => ..., 'data-x' => ...]; class and style merge into what the options set. Several calls add up.
$extraUnused.

Example

Name Stock
Desk lamp 12
Office chair 0

Code

$rows = [
    1 => ['id' => 1, 'name' => 'Desk lamp', 'stock' => 12],
    2 => ['id' => 2, 'name' => 'Office chair', 'stock' => 0],
];
return table($rows, [
        'no_pages' => 1,
        'tr' => fn ($row, $id) => $row['stock'] ? '' : ['class' => 'text-muted'],
        'td' => fn ($row, $name) => $name === 'stock' && ! $row['stock'] ? 'class="text-danger"' : '',
    ])
    ->tr(fn ($row) => $row['stock'] > 10 ? ['class' => 'fw-bold'] : '')
    ->text('name')
    ->text('stock');

Notes

  • Known bug, fixed behind a flag: by default render() never reads _callbacks, so tr() and td() change nothing (the example has no bold row unless the flag is on). yf_table2::$TR_TD_CALLBACKS = true ($PROJECT_CONF['table2']['TR_TD_CALLBACKS'], Settings / Widget fixes; the 4.0 default) applies them. Without it use the options: the tr option gets ($row, $id), the td option ($row, $field, $row_id); both return an attribute string or ['class' => ..., 'style' => ...].
  • For row colours there is the tr_variant option of render().

See also: table/render, table/column_options