Tables

The table class handles creation and manipulation of HTML5 tables.

Each table consists of three sections: a header, body and footer. Each section can generate rows, which in turn generate cells.

Begin by creating the table.

$table = $this->table();

If you’re not using the WordPress plugin base, then specify the complete table namespace.

$table = new \plainview\sdk\table\table();

Why not give it a caption?

$table->caption()->text( 'This is the caption' );
$table->caption()->textf( 'This is an sprintf caption created %s', time() );

The header

This is usally the section where the columns are defined.

First, fetch a new row.

$row = $table->head()->row();

In this row, create a table heading cell.

$row->th()->text( 'This is a cell heading' );
$row->th()->textf( 'A %s heading', 'great' );

The cells can be used with unique identifiers. Using a cell ID you can retrieve the same cell later, instead of saving it in a variable.

$cell = $row->th()->text( 'Cell text' );
$cell->text( 'Updated text' );

Is the same as:

$row->th( 'cell123' )->text( 'Cell text' );
$row->th( 'cell123' )->text( 'Updated text' );

The body

Functionally similar to the header, but here is where you would use td() cells, instead of th().

$row = $table->body()->row();
$row->td()->text( 'This is great data' );
$row->td( 'cell123' )->text( 'This data is for the 123 column' );

Output the table as text

echo $table;

Cell ordering

If you choose not to use cell ID’s, then you have to create data cells in the same order as the header cells.

$row = $table->head()->row();
$row->th()->text( 'This is the first column' );
$row->th()->text( 'This is the second column' );
$row = $table->body()->row();
$row->th()->text( 'This is the first data' );
$row->th()->text( 'This is the second data' );

If you instead use IDs, the data can be created in any order.

$row = $table->head()->row();
$row->th( 'column1' )->text( 'This is the first column' );
$row->th( 'column2' )->text( 'This is the second column' );
$row = $table->body()->row();
$row->th( 'column2' )->text( 'This is the second data' );
$row->th( 'column1' )->text( 'This is the first data' );

Leave a Reply

Your email address will not be published. Required fields are marked *