ar\html\table

This module provides a simple way to generate (data) tables. You can create a table object with the ar('html')->table() method and then use the methods described below.

Example 1 - a simple table:

<pinp>
echo ar('html')->table(
array(
array( 'a', 'b' ),
array( 'c', 'd' )
)
);
</pinp>

Will ouput:

<table>
 <tbody>
  <tr class="tableFirst tableOdd">
   <td class="tableFirst tableOdd">a</td>
    <td class="tableLast tableEven">b</td>
   </tr>
   <tr class="tableLast tableEven">
    <td class="tableFirst tableOdd">c</td>
    <td class="tableLast tableEven">d</td>
   </tr>
 </tbody>
</table>

You can add a caption tag, a table head and foot, row headings and col / colgroup tags.

Example 2 - an accessible data table

<pinp>
 $headers    = array( '', 'Income', 'Cost', 'Net Result' );
 $tableData       = array(
  array( '200', '150', '50'),
  array( '180', '140', '40')
 );
 $footers    = array( 'Total', '380', '290', '90');
 $rowHeaders = array( '2010', '2009' ); 
echo ar('html')->table( $tableData )
  ->head( $headers )
  ->foot( $footers )
  ->rowHeaders( $rowHeaders );
</pinp>

Results in:

<table>
 <thead>
  <tr>
   <th class="tableFirst tableOdd"></th>
   <th class="tableEven">Income</th>
   <th class="tableOdd">Cost</th>
   <th class="tableLast tableEven">Net Result</th>
  </tr> </thead>
 <tfoot>
  <tr>
   <td class="tableFirst tableOdd">Total</td>
   <td class="tableEven">380</td>
   <td class="tableOdd">290</td>
   <td class="tableLast tableEven">90</td>  </tr>
 </tfoot>
 <tbody>
  <tr class="tableFirst tableOdd">
   <th scope="row">2010</th>
   <td class="tableFirst tableOdd">200</td>
   <td class="tableEven">150</td>
   <td class="tableLast tableOdd">50</td>
  </tr>
  <tr class="tableLast tableEven">
   <th scope="row">2009</th>
   <td class="tableFirst tableOdd">180</td>
   <td class="tableEven">140</td>
   <td class="tableLast tableOdd">40</td>  </tr>
 </tbody>
</table>

Or:

Income Cost Net Result
Total 380 290 90
2010 200 150 50
2009 180 140 4

The ar_html::table() method generates an object of class ar_html_table, which implements all the functionality of the ar_htmlElement class (and thus the ar_xmlElement class). This means that you can modify anything in the result, using dom-like methods:

<pinp>
$table = ar('html')->table( $tableData );
$table->tbody->tr->setAttribute('class', array(
'extraClass' => 'myClass'
) );
$table->tfoot->lastChild->appendChild(
ar('html')->el('tr',
ar('html')->el('td',
array( 'colspan' => 4 ),
'This is also in the tables foot'
)
)
);
echo $table;
</pinp>

You can override the classes generated by the ar_html::table() method using their group names: 'tableOddEven' and 'tableFirstLast', e.g:

<pinp>
$table = ar('html')->table( $tableData );
$table->tbody->tr->setAttribute(
'class',
array(
'tableOddEven' => ar::listPattern( '(even odd?)*' )
)
);
echo $table;
</pinp>

This will result in a table with the rows in its table bodies starting with class even instead of odd.

methods

(object) body Sets the data for the body of the table.
(object) caption Sets the content for the caption.
(object) cols Sets the col / colgroup information for the table.
(object) foot Sets the content for the table foot.
(object) head Sets the content for the table head.
(object) rowHeaders Sets the content for the row headings.

body

(object) $htmlTable->body( $content )

(mixed) $content An array of data or a string.

Adds a table body to the table, with the contents generated from the $content parameter. This can be a string, in which case the table body will contain a single row with a single cell containing that string. It can also be an array of strings, in which case the body will contain as many rows as entries in the array, with each row containing a single cell with the value of that entry. Finally it can be an array of arrays, in which case the body will contain as many rows as entries in the main array and each row will contain as many cells as entries in the sub-array.

The function returns the ar_html_table object.

caption

(object) $htmlTable->caption( $content, $attributes = null )

(string) $content The content for the caption tag.
(array) $attributes The attributes for the caption tag.

Sets the caption for the table. Returns the ar_html_table object. 

cols

(object) $htmlTable->cols( $content, ... )

(string) $content The col or colgroup tags.

Sets the col and/or colgroup information for the table. You must generate the col and colgroup tags with the ar_html::tag() method. You can add as many arguments as you need. When the table is converted to a string, these tags are inserted after the caption and before the table head. The method doesn't strictly enforce that only col or colgroup tags are inserted so be careful.

Returns the ar_html_table object.

foot

(object) $htmlTable->foot( $content, $attributes = null )

(string) $content The contents of the table foot.
(array) $attributes The attributes for the table foot (tfoot)

Adds a table foot to the table. $content can be either a string or an array of strings. If there is already a table foot, it will add another row to it.

Returns the ar_html_table object.

head

(object) $htmlTable->head( $content, $attributes = null )

(string) $content The contents of the table head.
(array) $attributes The attributes for the table head (thead)

Adds a table head to the table. $content can be either a string or an array of strings. If there is already a table head, it will add another row to it.

Returns the ar_html_table object.

rowHeaders

(object) $htmlTable->rowHeaders( $list, $attributes = null )

(array) $list The contents of the row headings.
(array) $attributes The attributes for therow headings.

Adds a heading cell ( th ) as the first element of each row in the table body. The row headers will be inserted before the first cell of each row of the last table body (TBODY) of the table, in order.

Returns the ar_html_table object.