Welcome Guest, Not a member yet? Register   Sign In
MY_Table
#1

[eluser]briguy[/eluser]
I made a small change to the Tables Library that allows you to assign an anchor to to a specified column.
This allows you to make quick and dirty drill down functionality.
There is no error checking and a lot more can be done, however I found in useful and though I would post.

In the comments below, you will see the code necessary to put in the view template.

Basically, you create a Table Template and add extra fields that indicate which columns should become a link.

This works with a database result set not an user created Array.

Installation:
Save the below code as My_Table.php and put in your application/libraries directory.
Follow the direction below on how to configure the table template in the view.

Brian

EDIT 07/30/2009: I changed the code slightly because it was displaying wrong as. $this->path was causing some problem in displaying the code.

Code:
<?PHP
/**
   * MY_Table
   *
   * Extends and Overrides CI_Table methods
   *
   */
class MY_Table extends CI_Table {

    function My_Table()
    {
            parent::CI_Table();
    }
    
    
    
    /**
     * Set table data from a database result object
     *
     * @access    public
     * @param    object
     * @return    void
     */
    function _set_from_object($query)
    {

//  this should be added to the template    
//URL is the url to be use to drill into the item, ID is appended on to the URL, VAL is what the link text is.
/*                        
        $links = array (
                        'ID'   => array('URL' => 'person/getbyid',     'ID' => 'ID',       'VAL' => 'ID'),
                        'NAME'  => array('URL' => 'person/getbyid',    'ID' => 'ID',   'VAL' => 'NAME')
                    );    
                    
                     $tmpl = array (
                        'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">',
                                       'links'  => $links        
                       );    
        $this->table->set_template($tmpl); ?&gt;
        
*/            
// above should be added to the template        


        $this->CI =& get_instance();
        $this->CI->load->config('config');
        $this->path = $this->CI->config->item('base_url');

        $this->_compile_template();    

        if ( ! is_object($query))
        {
            return FALSE;
        }
        
        // First generate the headings from the table column names
        if (count($this->heading) == 0)
        {
            if ( ! method_exists($query, 'list_fields'))
            {
                return FALSE;
            }
            
            $this->heading = $query->list_fields();

        }
                
        // Next blast through the result array and build out the rows
        
        if ($query->num_rows() > 0)
        {
            $links = $this->template['links'];  //get the links from the template
            foreach ($query->result_array() as $row)
            {
              foreach ($row as $key=>$value)
              {
                 $path = $this->path;
                  if ($links[$key])
                   {
                    $rownew[] = '<a href="'.$path.$links[$key]['URL'].'/'.$row[$links[$key]['ID']] .'">'.$row[$links[$key]['VAL']].'</a>';                        
                   }
                  else
                   {
                      $rownew[] = $value;                
                   }
               }
              
               $this->rows[] = $rownew;
               $rownew = array();
              
            }
        }
    }

    
    
    
    
}


?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB