Welcome Guest, Not a member yet? Register   Sign In
Specify class when adding data to table
#1

[eluser]Jay Gridley[/eluser]
Hi foks,

I am using HTML Table class in CI and I am filling table using table->add_row() method..

Is possible to specify class for each table cell? Something like

Code:
$item1 = array('data' => 'mydata', 'class' => 'first');
$item2 = array('data' => 'mydata2', 'class' => 'second');
.
.
.
$this->table->add_row($item1, $item2, ...);

Resulting in:
<tr>
<td class="first">myData</td>
<td class="second">myData2</td>
.
.
.
</tr>

Any ideas? Thanks a lot!
#2

[eluser]falkencreative[/eluser]
What's the reasoning for this? Just for zebra striping? (highlighting alternate rows in the table) You can use the "cell_start" and "cell_start_alt" within set_template() for this (http://ellislab.com/codeigniter/user-gui...table.html).

If not... I imagine you could extend the table library to do this. I did something like this just recently, except I was adding attributes to the <th> tags. It's not 100% ready to be released yet, but with a bit more tweaking I should be able to release it and you could do something based on the ideas in my code.
#3

[eluser]Jay Gridley[/eluser]
Reason is aligning cells due to its content...text to left, numbers to right etc..let me know when you are ready to release it, I will look at it for inspiration :-)
#4

[eluser]falkencreative[/eluser]
I should be finishing it up shortly (in the next day or two) so I'll let you know.
#5

[eluser]falkencreative[/eluser]
Here's how I extended the library to be able to add attributes (classes, ids, etc) to the <th> tag:

Let me know if you have questions. I'll probably be writing a proper blog post on this, and perhaps if I have the time modifying it to add the option to add classes to each of the <td> tags as well.

"MY_Table.php" (placed within application/libraries, extends the Table library)
Code:
&lt;?php

class MY_Table extends CI_Table
{

    function MY_Table()
    {
        parent::CI_Table();
        $CI =& get_instance();
        
        $this->template['heading_cell_start_open'] = '<th';
        $this->template['heading_cell_start_close'] = '>';
        $this->template['heading_attributes'] = false;
    }
    
    /**
     * Set the table heading
     *
     * Can be passed as an array, discreet params, or multiple arrays
     *
     * @access    public
     * @param    mixed
     * @return    void
     */
    function set_heading()
    {
        $args = func_get_args();
        
        // check for default arguements: either one array of all header items, or multiple strings
        if ( count($args) == 1 || (count($args) > 1 && is_array($args[1]) == false) )
        {        
            $this->heading = (is_array($args[0])) ? $args[0] : $args;
        }
        // otherwise, we're dealing with multiple arrays
        else
        {
            $this->heading = $args;
            $this->template['heading_attributes'] = true;
        }
    }

    /**
     * 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;    

            /* start edits
               ------------------------------------------------------- */              
            if ($this->template['heading_attributes'] == true)
            {
                // use header attributes
                for ($i = 0; $i < count($this->heading); $i++)
                {
                    $out .= $this->template['heading_cell_start_open'];
                    if ($this->heading[$i] != '')
                    {
                        $out .= ' ' . $this->heading[$i]['attributes'];
                    }
                    $out .= $this->template['heading_cell_start_close'];
                    $out .= $this->heading[$i]['name'];
                    $out .= $this->template['heading_cell_end'];
                }
            }  
            else
            {
                // no header attributes
                for ($i = 0; $i < count($this->heading); $i++)
                {
                    $out .= $this->template['heading_cell_start_open'];
                    $out .= $this->template['heading_cell_start_close'];
                    $out .= $this->heading[$i];
                    $out .= $this->template['heading_cell_end'];
                }
            }
            /* end edits
               ------------------------------------------------------- */

            $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_';
            
                $out .= $this->template['row_'.$name.'start'];
                $out .= $this->newline;        
    
                foreach($row as $cell)
                {
                    $out .= $this->template['cell_'.$name.'start'];
                    
                    if ($cell === "")
                    {
                        $out .= $this->empty_cells;
                    }
                    else
                    {
                        $out .= $cell;
                    }
                    
                    $out .= $this->template['cell_'.$name.'end'];
                }
    
                $out .= $this->template['row_'.$name.'end'];
                $out .= $this->newline;    
            }
        }

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

[eluser]falkencreative[/eluser]
To test this out, here's a sample controller/view to demonstrate to use the revised library:

My controller:
Code:
&lt;?php

class Welcome extends Controller
{    
    function index()
    {
        $this->load->library('table');
        
        // set headers
        // use name/attribute pairs:
        $this->table->set_heading(
            array('name' => 'col1', 'attributes' => 'class="red"'),
            array('name' => 'col2', 'attributes' => 'class="red"'),
            array('name' => 'col3', 'attributes' => '')
            );
        
        // use standard headers without attributes    
        /*$this->table->set_heading(
            array('col1', 'col2', 'col3')
            ); */
            
        // populate with data    
        $data = array(
             array('Fred', 'Blue', 'Small'),
             array('Mary', 'Red', 'Large'),
             array('John', 'Green', 'Medium')
             );
            
        $data['table'] = $this->table->generate($data);
        $this->load->view('hello', $data);
    }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

My view:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
&lt;html&gt;
    &lt;head&gt;    
        &lt;title&gt;Sample&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
        
        &lt;style type="text/css"&gt;
            th { text-align: left; }
            th.red { color:red; }
        &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;?php echo $table; ?&gt;
    &lt;/body&gt;
&lt;/html&gt;




Theme © iAndrew 2016 - Forum software by © MyBB