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
| Option | Meaning |
|---|---|
desc | Header text; the second argument does the same. Default: the field name with _ as spaces, first letter upper case. |
data | Value => text map ([1 => 'Draft', 2 => 'Published']) or a fixed string; @other_field prints that field of the row. |
link | Turns 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_name | Field whose value replaces %d in link, usually id. |
link_params | Comma-separated fields whose %<field> placeholders are replaced; default every field of the row. |
rewrite | true passes the link through url(); user / admin use url_user() / url_admin(). |
link_title | Title attribute of the link; default the cell text. |
link_trim_width | Longest link text in characters (default conf link_trim_width, 100), longer text ends with .... |
translate | Passes the value through t(). |
max_length | Cuts the text to this many bytes. |
nowrap | The cell does not wrap: spaces become non-breaking ones (links do it by default; false turns it off). |
icon | CSS class of an <i> icon before the text. |
raw | true prints the value as it is: no HTML escaping (with ESCAPE_TEXT), braces kept. |
badge / label | Wraps the text in a badge or label of that colour (success, danger...), or a value => colour map. |
hidden_data | Extra fields printed in a hidden container next to the cell (%field names). |
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(defaultfalseuntil 4.0) to HTML-escape everytext()cell, and passrawfor a column that holds HTML on purpose. Template braces are always escaped unlessrawis 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