Welcome Guest, Not a member yet? Register   Sign In
html table improvement
#1

[eluser]rafael apocalypse[/eluser]
Hello Everybody,

I'm trying to improve the html table library, so it could accept a variable witch will be used to adjust the width of the cel.

Is anybody knows a easy way to do this with the need to rewrite all the table library?

regards,

RA
#2

[eluser]eoinmcg[/eluser]
Hi,

You can set a table template, which allows you complete control over all aspects of your table.

http://ellislab.com/codeigniter/user-gui...table.html

Check the above page in the user guide, about half way down
#3

[eluser]rafael apocalypse[/eluser]
I'm looking for something with more control, for example I need to ajust a variable witdh for each column in the table, and just use another template doesn't allow me to do this.

I'm thinking in use something like this: http://ellislab.com/forums/viewthread/102564/ but using real table to show tabled data
#4

[eluser]Unknown[/eluser]
I'm looking for the same thing, any other suggestions?
#5

[eluser]eoinmcg[/eluser]
Aha, rafael apocalypse!

I guess I didn't quite digest the bit about column width *ahem!* :cheese:

You wouldn't need to rewrite the entire table library and, as the commments in that thread you posted argue, there are a few problems with that approach.

I had a quick look at the table library, which is a very well thought out piece of code, and came up with the following extension to the library. It adds a class name (<th class="cell_0"> etc) to each of the th tags, allowing you to do your magic in the css:

Code:
&lt;?php

class MY_Table extends CI_Table
{

    function MY_Router(){
        parent::CI_Table();
    }

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

    /**
     * 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 $count => $heading)
            {
                // ADDED: adds a class name to each th allowing you to add css style col width etc
                $out .= str_replace('>', ' class="cell_'.$count.'">', $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_';
            
                $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;
    }
}

Basically, I just added a comment and a small change on lines 67 & 68.

Hope this helps.
#6

[eluser]garymardell[/eluser]
I never understood why people don't just stick to having the html written rather than generated. Doesn't leave much flexibility having it generated.
#7

[eluser]eoinmcg[/eluser]
@garymardell, of course it's a question of personal preference but on a project that requires more than a couple of tables I wouldn't fancy writing and maintaining the html for all of them Tongue
#8

[eluser]sophistry[/eluser]
i'm surprised this never made it into the core... it's pretty good if i do say so myself. Wink
new generate method to pass arrays to table to define th and tds individually
#9

[eluser]rafael apocalypse[/eluser]
So do I!

now I really don't know what to do, stop to write my own code, and start to use this, or keep going with my library…

Changing the subject, is there a place where we can ask for some code to be in the core, or some way to contribute with ci development?

regards,




Theme © iAndrew 2016 - Forum software by © MyBB