Welcome Guest, Not a member yet? Register   Sign In
Add columns to HTML Table Class
#1

[eluser]danilm[/eluser]
Hi, I'm using HTML Table Class to create a table with the result of a select from database as well as pagination class.

Everything goes fine except that I cannot include a link on every row to edit or delete it.

Is there any way to add columns once the table is generated? or is there anyway to add values to the parameter that stores the query result from database?

Thanks
#2

[eluser]hvalente13[/eluser]
I guess you have to read the Table Class deeply in User Guide.

Table Class

But here goes a tip:

When you load the table class, then you add rows, and the table will be built dynamically. So if you alter your code in the add_row line, you can manage to add more columns to it, like this:
Code:
$this->table->add_row('Column1','Column2','Edit_Action_Column','Del_Action_Column');
$this->table->add_row($data1,$data2,anchor('Edit',base_url().'path/to/action_edit'),anchor('Edit',base_url().'path/to/action_del'));

But you can find all this info in the User Guide, that's a very good one!
#3

[eluser]danilm[/eluser]
Thanks for your reply,

this is the way i create the table(controller):

$this->load->model('gestionmodel');
$this->data['results'] = $this->gestionmodel->listarpaquetes($config['per_page'],$this->uri->segment(3));
$this->load->library('table');
$tmpl = array (
'table_open' => '<table cellspacing="2" cellpadding="0" width="100%">',

'heading_row_start' => '<tr>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th class=titulos_lista>',
'heading_cell_end' => '</th>',

'row_start' => '<tr class=par>',
'row_end' => '</tr>',
'cell_start' => '<td class=centrado>',
'cell_end' => '</td>',

'row_alt_start' => '<tr class=impar>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td class=centrado>',
'cell_alt_end' => '</td>',

'table_close' => '</table>'
);

$this->table->set_template($tmpl);

$this->table->set_heading('ID', 'Titulo', 'Fecha Inicio', 'Fecha Fin','Fecha Auto Publicacion','Fecha Auto Despublicacion','Publicado','Editar');

$this->load->view('listado',$this->data);

And in my view:
&lt;?php echo $this->table->generate($results); ?&gt;

What you're saying is to add rows instead heading? and then generating? do the table generates all the results without using foreach?

thanks again
#4

[eluser]hvalente13[/eluser]
Yes it does generate the table without foreach, but if you want to add more interactivity to your rows, like edit or delete actions, your data['results'] array must be configured to do that.

I supose that your model is only retrieving the database results, but if you work it before passing it to controller you can manage the expected result as well:

Code:
//Model database query before this
$query = $this->db->query('mytable',$limit,$offset);
if($query->num_rows()>0){
  foreach($query->result() as $row){
     $data[] = array(
        $row->field1,
        $row->field2,
        //... all field results you may want
        anchor('Edit',base_url().'path/to/action_edit'),
        anchor('Edit',base_url().'path/to/action_del'),
     );
  }
}
return $data;

Of course if you want to work this in your controller you have to use the add_row method in a foreach loop!
Code:
// in the controller
if($data['results']->num_rows()>0){
  foreach($data['results']->result() as $row){
     $this->table->add_row($data1,$data2,anchor('Edit',base_url().'path/to/action_edit'),anchor('Edit',base_url().'path/to/action_del'));
  }
}

The heading is ok, if your table template is assuming that!
#5

[eluser]danilm[/eluser]
ok,I see

thanks for your help!




Theme © iAndrew 2016 - Forum software by © MyBB