text() table()

A column that prints one field of each row as text, optionally as a link, translated, shortened or with a badge.

table()->text($name, $desc = '', $extra = [])

Options

OptionMeaning
descHeader text; the second argument does the same. Default: the field name with _ as spaces, first letter upper case.
dataValue => text map ([1 => 'Draft', 2 => 'Published']) or a fixed string; @other_field prints that field of the row.
linkTurns the cell into a link. %d is replaced by the value of this column (or of link_field_name), %<field> by that field of the row, both URL-encoded.
link_field_nameField whose value replaces %d in link, usually id.
link_paramsComma-separated fields whose %<field> placeholders are replaced; default every field of the row.
rewritetrue passes the link through url(); user / admin use url_user() / url_admin().
link_titleTitle attribute of the link; default the cell text.
link_trim_widthLongest link text in characters (default conf link_trim_width, 100), longer text ends with ....
translatePasses the value through t().
max_lengthCuts the text to this many bytes.
nowrapThe cell does not wrap: spaces become non-breaking ones (links do it by default; false turns it off).
iconCSS class of an <i> icon before the text.
rawtrue prints the value as it is: no HTML escaping (with ESCAPE_TEXT), braces kept.
badge / labelWraps the text in a badge or label of that colour (success, danger...), or a value => colour map.
hidden_dataExtra fields printed in a hidden container next to the cell (%field names).

Every column also takes the common column options (width, th_desc, header_tip, td...).

Example

# Name Status Price, $ Comment
1 Desk lamp Published 24.90 Warm white, 3 levels
2 Office chair Draft 149.00 Back order
3 Notebook A5 Published 3.50

Code

$rows = [
    ['id' => 1, 'name' => 'Desk lamp', 'status' => 2, 'price' => '24.90', 'comment' => 'Warm white, 3 levels'],
    ['id' => 2, 'name' => 'Office chair', 'status' => 1, 'price' => '149.00', 'comment' => 'Back order'],
    ['id' => 3, 'name' => 'Notebook A5', 'status' => 2, 'price' => '3.50', 'comment' => ''],
];
return table($rows, ['no_pages' => 1])
    ->text('id', '#')
    ->text('name', ['link' => '#product-%d', 'link_field_name' => 'id', 'link_title' => 'Open the product'])
    ->text('status', ['data' => [1 => 'Draft', 2 => 'Published'], 'badge' => [1 => 'warning', 2 => 'success']])
    ->text('price', 'Price, $', ['nowrap' => 1])
    ->text('comment', ['max_length' => 12]);

Notes

  • Values come from the database as they are: set $PROJECT_CONF['table2']['ESCAPE_TEXT'] = true (default false until 4.0) to HTML-escape every text() cell, and pass raw for a column that holds HTML on purpose. Template braces are always escaped unless raw is set.
  • The second argument may be the options array: text('name', ['desc' => 'Name']).
  • link() is text() with the link as an argument, text_padded() indents tree rows.

Bootstrap 3 and 5

Links use CLASS_BTN_MINI: btn btn-default btn-xs under Bootstrap 3, btn btn-outline-secondary btn-sm under Bootstrap 5; badge / label become badge text-bg-<colour> under Bootstrap 5.

See also: table/link, table/func, demo:table/filter