• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] How to get colspan when using the table library

#1
[eluser]123wesweat[/eluser]
Hi,

i can't find the use of colspan in http://ellislab.com/codeigniter/user-gui...table.html

Any work around??? and still using the library

#2
[eluser]ShiverCube[/eluser]
The Table Library doesn't make any use of colspan. I'm currently looking into extending the library to automatically set the colspan for rows which contains a smaller number of values than there are columns. I'm not sure whether that's the same problem you are facing, but if I find a solution I will post it here.

#3
[eluser]ShiverCube[/eluser]
Well here's my solution to the problem I was having. I'm not sure whether it will help you at all. Basically I just extended the Table Library and modified the generate() method, replacing the existing section for building the table rows with the the following.

Code:
// Build the table rows
if (count($this->rows) > 0)
{
    $column_count = count($this->heading) - 1;
    
    $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;        

        for ($i = 0, $len = count($row), $real_len = $len - 1; $i < $len; ++$i)
        {
            $cell = $row[$i];
            
            if ($i == $real_len && $i < $column_count)
            {
                $out .= preg_replace('/^(.+)>$/im', '${1} colspan="'.($column_count + 1).'">',
                    $this->template['cell_'.$name.'start']);
            }
            else
            {
                $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;    
    }
}

What this does is set the colspan so that all rows remain the same length. So if for example the following was used:

Code:
$this->table->set_heading('Name', 'Color', 'Size');
echo $this->table->generate(array(
    array('Name', 'Color', 'Size'),
    array('Fred'),
    array('Mary', 'Red', 'Large'),
    array('John', 'Green', 'Medium')
));

Then the 2 row will have its colspan set to 3. If that doesn't help you then let me know you problem in more detail so that I can help Smile

#4
[eluser]123wesweat[/eluser]
tx, i am using your code by creating a MY_Table library and a generateColspan() method.

Another question:
How do i get a <td class="some-style">???

I am trying to recreate cellspacing="15" which i can't use in the <table ....> tag as i am also using a general reset.css.

Anym thoughts??

#5
[eluser]ShiverCube[/eluser]
Well the Table library supports basic templating:

Code:
$this->table->set_template(
    'table_open' => '<table border="0" cellpadding="0" cellspacing="15">',
    'cell_start' => '<td class="someclass">'
);


But if you need to set a class for each cell individually, then you will have to extend the class again.

#6
[eluser]123wesweat[/eluser]
yeah, shivercube i noticed the table library just supports basic temmplating

so i have altered my strategy,
here the table tags/classes are in a seperate view file called detail_view
Code:
//data from db based on id
          $job_id = $this->uri->segment(3);
          $job_details = $this->jobs_model->get_job($job_id);
          $detail_data = array(
                  'title' => $job_details['title'],
etc.
          );
//          load the table view with the data and parse it as data!
           $detail_view = $this->load->view('jobs/detail_view', $detail_data ,true);
//           use the parse data in the template
          $this->data = array(
                  //comes from the db
                  'titletag'         => "title",
                  'metadescription'     => "Your page's description here",
                  'metakeywords'        => "keywords, keywords, keywords",
                  'content'             => $detail_view,
                  
          );
          $this->load->view('template', $this->data);

might be helpfull for others

#7
[eluser]Unknown[/eluser]
This might be helpful as well:

Code:
// 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;        
                
                if(in_array('colspan', array_keys($row), true))
                {
                    $out .= str_replace('<td', '<td colspan="' . $row['colspan'] . '" class="' . $row['class'] . '"', $this->template['cell_'.$name.'start']);
                
                    if ($row['data'] === "")
                    {
                        $out .= $this->empty_cells;
                    }
                    else
                    {
                        $out .= $row['data'];
                    }
                    
                    $out .= $this->template['cell_'.$name.'end'];
                    
                    continue;
                }
                
                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;    
            }
        }

This is pretty basic code for making sure the supplied colspan is used. You can modify it by removing the class attribute but that's one of those things I need for my application to look nicer.

The code above can be executed with this command from the view:

Code:
$cell_data = array(
        'data' => 'Personal details',
        'class' => 'tr_between',
        'colspan' => 2
    );
    
    $this->table->add_row($cell_data);


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.