Skip to content
Derek Jones edited this page Jul 5, 2012 · 3 revisions

Extension for the CI HTML Table class. Overrides the generate function, adding the ability to add widths to the first row of table cells, column-based classes on table cells, and IDs to rows. Using a combination of row IDs and table cell classes, you can target any individual cell in your table.

Feel free to adapt as you need. Your mileage may vary, but it works ok for me. :)

NOTE: if you want to span columns or rows, this extension probably won't help you.

how to use

Build your table normally (see the user guide).

To set widths, create an array of widths and assign them to the table:

$this->table->set_widths($widths_array); 

To set classes for each table cell in the table columns, create an array of classes and assign them to the table:

$this->table->set_classes($classes_array); 

To set IDs for all the table rows, add a 'row_id' => $value to each row of your table data. It will appear as #tr-$value -- i.e., if row_id = 45, it will appear as #tr-45.

$tabledata[] = array('row_id' => $row_id_value, ...);
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Table extends CI_Table {

    var $classes    = array();
    var $widths        = array();
    
    
    function MY_Table()
    {
        log_message('debug', "MY Table Class Initialized");
    }

    // --------------------------------------------------------------------

    /**
     * Set the table widths and classes
     *
     * Can be passed as an array or discreet params
     *
     * @access    public
     * @param    mixed
     * @return    void
     */

    function set_classes()
    {
        $args = func_get_args();
        $this->classes = (is_array($args[0])) ? $args[0] : $args;
    }

    function set_widths()
    {
        $args = func_get_args();
        $this->widths = (is_array($args[0])) ? $args[0] : $args;
    }

    // --------------------------------------------------------------------


    /**
     * Generate the table
     *
     * @access    public
     * @param    mixed
     * @return    string
     */
    function generate($table_data = NULL)
    {
        // The table data can optionally be passed to this function
        // either as a database result object or an array
        if ( ! is_null($table_data))
        {
            if (is_object($table_data))
            {
                $this->_set_from_object($table_data);
            }
            elseif (is_array($table_data))
            {
                $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
                $this->_set_from_array($table_data, $set_heading);
            }
        }
    
        // Is there anything to display?  No?  Smite them!
        if (count($this->heading) == 0 AND count($this->rows) == 0)
        {
            return 'Undefined table data';
        }
    
        // Compile and validate the template date
        $this->_compile_template();
    
    
        // Build the table!
        
        $out = $this->template['table_open'];
        $out .= $this->newline;        

        // Add any caption here
        if ($this->caption)
        {
            $out .= $this->newline;
            $out .= '<caption>' . $this->caption . '</caption>';
            $out .= $this->newline;
        }

        // Is there a table heading to display?
        if (count($this->heading) > 0)
        {
            $out .= $this->template['heading_row_start'];
            $out .= $this->newline;        

            foreach($this->heading as $key => $heading)
            {
                $replace = " ";
                if( ! empty ($this->widths[$key])) {
                    $replace .= "width='{$this->widths[$key]}' ";
                }
                if( ! empty ($this->classes[$key])) {
                    $replace .= "class='{$this->classes[$key]}' ";
                }
                $replace .= ">";
                
                $out .= str_replace(">", $replace, $this->template['heading_cell_start']);
                $out .= $heading;
                $out .= $this->template['heading_cell_end'];
            }

            $out .= $this->template['heading_row_end'];
            $out .= $this->newline;                
        }

        // Build the table rows
        if (count($this->rows) > 0)
        {
            $i = 1;
            foreach($this->rows as $row)
            {
                if ( ! is_array($row))
                {
                    break;
                }
            
                // We use modulus to alternate the row colors
                $name = (fmod($i++, 2)) ? '' : 'alt_';
            
                // see if we're passing row_id
                if( array_key_exists('row_id',$row) ) {
                    $row_id = $row['row_id'];
                    unset($row['row_id']);

                    $rowstart = $this->template['row_'.$name.'start'];
                    $find = "<tr";
                    $replace = "{$find} id='tr-{$row_id}' ";
                    $rowstart = str_replace($find,$replace,$rowstart);
                    $out .= $rowstart;    
                } else {
                    $out .= $this->template['row_'.$name.'start'];                                    
                }

                $out .= $this->newline;        
    
                $j = 0;
                foreach($row as $cell)
                {

                    $replace = " ";
                    if( ! empty ($this->classes[$j])) {
                        $replace .= "class='{$this->classes[$j]}' ";
                    }
                    $replace .= ">";
                    $out .= str_replace(">", $replace, $this->template['cell_'.$name.'start']);

                    if ($cell === "")
                    {
                        $out .= $this->empty_cells;
                    }
                    else
                    {
                        $out .= $cell;
                    }
                    
                    $out .= $this->template['cell_'.$name.'end'];
                    
                    $j++;
                }
    
                $out .= $this->template['row_'.$name.'end'];
                $out .= $this->newline;    
            }
        }

        $out .= $this->template['table_close'];
    
        return $out;
    }
    

}
Clone this wiki locally