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);
Johnjohn@doe.comguests
Mickaelmickael@doe.comregistered
Simonsimon@doe.combanned

The second parameter is an associative (or not) array of properties you want to expose in the table.

$props = array("name" => "First Name", "email" => "Email address", "group" => "Group name");
$table = Html::table($data, $props);
First NameEmail addressGroup name
Johnjohn@doe.comguests
Mickaelmickael@doe.comregistered
Simonsimon@doe.combanned

The third parameter represents html attributes, were you can set module level ones:

$table = Html::table($data, $props, array("bordered" => true, "alternator" => "success|info|warning"))
First NameEmail addressGroup name
Johnjohn@doe.comguests
Mickaelmickael@doe.comregistered
Simonsimon@doe.combanned

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"));
Registered users
First NameEmail addressGroup name
Johnjohn@doe.comguests
Mickaelmickael@doe.comregistered
Simonsimon@doe.combanned

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;
});
Registered users
First NameEmail addressGroup name
Johnjohn@doe.comguests
Mickaelmickael@doe.comregistered
Simonsimon@doe.combanned

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");
});
Registered users
First NameEmail addressGroup name
Johnjohn@doe.comguests
Mickaelmickael@doe.comregistered
Simonsimon@doe.combanned

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"]);
});
Registered users
First NameEmail addressGroup name
Johnjohn@doe.comguests
Mickaelmickael@doe.comregistered
Simonsimon@doe.combanned

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");
});
Registered users
Online First NameEmail addressGroup name
Johnjohn@doe.comguests
Mickaelmickael@doe.comregistered
Simonsimon@doe.combanned

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"]));
});
Registered users
Online First NameLengthEmail addressGroup name
John4john@doe.comguests
Mickael7mickael@doe.comregistered
Simon5simon@doe.combanned

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"));
Registered users
Online First NameLengthEmail addressGroup name
Total of 3 registered users
John4john@doe.comguests
Mickael7mickael@doe.comregistered
Simon5simon@doe.combanned