CodeIgniter: Ability to add HTML attributes to a table row such as class or id

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:

  1. Extend the Table class -> https://github.com/dalehurley/codeigniter-table-rows -> add MY_Table.php to application/libraries folder
  2. 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)
  3. 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);
    
    Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s