Skip to content
stevewest edited this page Mar 5, 2013 · 3 revisions

Basic Use

The TableBuilder class provides a way to easily construct table like structures for rendering in a variety of formats.

Creating a table

<?php
use \FuelPHP\Common;

$table = new Table;
$table->setAttributes(array('id' => 'table-base'))
  ->createRow(Table\EnumRowType::Header)
    ->addCell('First cell!')
    ->addCell('Second cell!')
  ->addRow(array('id' => 'first-row'))

  ->createRow()
    ->addCell('Now there is a second row')
    ->addCell('This one has an attribute', array('id' => 'has-attributes'))
  ->addRow()
	
  ->createRow(Table\EnumRowType::Footer)
    ->addCell('I am at the bottom!')
    ->addCell('Me too!')
  ->addRow();

The Table class itself can handle the creation of Cell and Row objects by itself but you can also build them manually if you wish.

Rendering a table

This table structure can then be passed through a table renderer to convert it into HTML or whatever format you desire. Currently there is only one simple renderer that uses HTML table, tr and td tags to create a table.

<?php
use FuelPHP\Common\Table\Render;

$renderer = new SimpleTable;

$html = $renderer->renderTable($table);

When the Table built above is rendered with SimpleTable it will produce a table that looks something like this:

<table id="table-base">
  <thead>
    <tr>
      <td>First cell!</td>
      <td>Second cell!</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Now there is a second row</td>
      <td id="has-attributes">This one has an attribute</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>I'm at the bottom!</td>
      <td>Me too!</td>
    </tr>
  </tfoot>
</table>

Implementing other methods of rendering

If you do not wish to use tables, or just don't like the way the included table renders work, you can implement your own by creating a subclass of FuelPHP\Common\Table\Render.

You can then use your new render by creating an instance of it and calling renderTable() on it.