It is annoying that CodeIgniter’s HTML Table class does not support the ability to add attributes to the table rows such as classes, ids and data-attr. Using something like the Twitter BootStrap you may be wanting to add classes to the rows so that you can style rows that are success, errors or warnings.
I decided that this is crazy and that the rows should work the same as the cells. I have posted two options on GitHub to overcome the problem:
- Extend the Table class -> https://github.com/dalehurley/codeigniter-table-rows -> add MY_Table.php to application/libraries folder
- Replace the Table class -> https://github.com/EllisLab/CodeIgniter/pull/2540 -> replace CI_Table.php in system/libraries folder (please comment and push for this to be pulled)
Despite which solution you chose you can add the HTML attributes to the table row using 1 of two methods:
$rows[]=array('data'=>$row, 'class'=>'warning'); echo $this->table->generate($rows);
or
$this->table->add_row(array('data'=>$row, 'class'=>'warning'));
So you may want to do something like:
$this->load->library('table'); foreach($people as $person) { $row_data=array($person->given_name,$person->family_name,$person->city); $this->table->add_row(array('data'=>$row_data, 'class'=>$person->status)); } $this->table->set_heading(array('Given name','Family Name', 'City')); echo $this->table->generate($rows);