A few improvements to Codeigniter’s HTML Table library. I really like this apparently under-appreciated library. It’s very useful for formatting tabular data. However, there are very limited options for styling generated tables, so I added some features to remedy that.
This library extends the core Table Class, letting you:
- moves table template into a config file
- allows you to set columns widths
- will auto-generate column classes on cells
- allows you to set row ids and classes
Installation
Drop the class in application/libraries and the config file in application/config. The library uses the MY_ prefix to extend the core Table class, so rename if you use a different prefix.
Table Config (optional)
Basically, this just moves the template array into a config file. I find it more convenient to use and modify this way.
Setting Column Widths
You can insert your own class names by setting $this→table→classes to an array of your class names.
$this->widths = array('c0','c1','c2');
Setting Column Classes
By setting ‘auto_class’ (boolean) will auto-generate column classes for table cells: ‘c0’, ‘c1’, etc. Auto class is true by default.
$this->auto_class = false;
Alternately, you can insert your own class names by setting $this→table→classes to an array of class names.
$this->classes = array('first_column','col2','lastcol');
Example class name generator (note: # of classes == # of columns in table):
$classes = array();
for($i = 0; $i < count($numberofcolumns); $i++ ) {
$classes[] = 'c'.$i;
}
Setting row ids
When building your arrays of row data, include an element “row_id” => ‘’, this will be parsed and inserted as the row id attribute, and deleted from the array before the row cells are generated.
$table_rows[] = array('row_id => 'row4', 'cell' => 'value');
Setting row classes
When building your arrays of row data, include an element “row_class” => ‘’, this will be parsed and inserted as the row class attribute, and deleted from the array before the row cells are generated.
$table_rows[] = array('row_class => 'green', 'cell1' => 'value', 'cell2' => 'value');
Have fun!