Html Tables See Docummentation
The first parameter is an array collection (like db results, or array of array) for table data:
$data = array( array("name" => "John", "email" => "john@doe.com", "group" => "guests"), array(...) ); $table = Html::table($data);
John | john@doe.com | guests |
Mickael | mickael@doe.com | registered |
Simon | simon@doe.com | banned |
The second parameter is an associative (or not) array of properties you want to expose in the table.
- If you provide an associative array,
keys
areproperties
, andvalues
are headernames
-
If you provide an simple array,
properties
arevalues
. However, ifautoheader
attribute is set totrue
, the class will also use values for header names
$props = array("name" => "First Name", "email" => "Email address", "group" => "Group name"); $table = Html::table($data, $props);
First Name | Email address | Group name |
---|---|---|
John | john@doe.com | guests |
Mickael | mickael@doe.com | registered |
Simon | simon@doe.com | banned |
The third parameter represents html attributes, were you can set module level ones:
condensed
,striped
,hover
andbordered
: add table-* css class to the table if set totrue
autoheader
: show properties names as table header namesalternator
: a|
separated string to generates css classes for each row
$table = Html::table($data, $props, array("bordered" => true, "alternator" => "success|info|warning"))
First Name | Email address | Group name |
---|---|---|
John | john@doe.com | guests |
Mickael | mickael@doe.com | registered |
Simon | simon@doe.com | banned |
Customize our table
Once data are set, you may need to customize how is rendered the table.
Lets start with caption
:
$table->caption("Registered users", array("class" => "muted"));
First Name | Email address | Group name |
---|---|---|
John | john@doe.com | guests |
Mickael | mickael@doe.com | registered |
Simon | simon@doe.com | banned |
header
. The callback provide current header value, all table properties, and html attributes:
$table->header("name", function($value, $properties, &$attributes){ return Html::icon("chevron-down")." ".$value; });
First Name | Email address | Group name |
---|---|---|
John | john@doe.com | guests |
Mickael | mickael@doe.com | registered |
Simon | simon@doe.com | banned |
With the cell
method, you set how is rendered a specific cell:
$table->cell("group", function($value, $row, &$attributes){ return Html::label($value, ($value === "banned") ? "important" : "info"); });
First Name | Email address | Group name |
---|---|---|
John | john@doe.com | guests |
Mickael | mickael@doe.com | registered |
Simon | simon@doe.com | banned |
The row
method lets you customize the entire row by reference if needed:
$table->row(function(&$row, $index, &$attributes){ $attributes["class"] = ($index % 2 ) ? "info" : "warning"; $row["email"] = Html::mail_to($row["email"]); });
First Name | Email address | Group name |
---|---|---|
John | john@doe.com | guests |
Mickael | mickael@doe.com | registered |
Simon | simon@doe.com | banned |
The prepend
or append
methods allow you to create new columns. First parameter is the property
name, second parameter the header
title:
$table->prepend("icon", "Online", function($value, $row, $attributes){ return Html::icon("eye-open"); }); $table->append("actions", "", function(){ return Html::button("#", "")->tooltip("Edit"); });
Online | First Name | Email address | Group name | |
---|---|---|---|---|
John | john@doe.com | guests | ||
Mickael | mickael@doe.com | registered | ||
Simon | simon@doe.com | banned |
The before
or after
methods allow you to add a column brefore / after a specific property (first parameter):
$table->after("name", "length", "Length", function($value, $row, $attributes){ return Html::badge(strlen($row["name"])); });
Online | First Name | Length | Email address | Group name | |
---|---|---|---|---|---|
John | 4 | john@doe.com | guests | ||
Mickael | 7 | mickael@doe.com | registered | ||
Simon | 5 | simon@doe.com | banned |
Finnaly, add a footer
for our table. The colspan is set automatically if you provide a string, or an array smaller than the number of cols:
$text = "Total of ".count($data)." users registered"; // can be an array $table->footer(text, array("class" => "text-success"));
Online | First Name | Length | Email address | Group name | |
---|---|---|---|---|---|
Total of 3 registered users | |||||
John | 4 | john@doe.com | guests | ||
Mickael | 7 | mickael@doe.com | registered | ||
Simon | 5 | simon@doe.com | banned |