初始化类
像CodeIgniter的其它类一样, 在控制器中使用$this->load->library 函数来初始化表格类:$this->load->library('table');
一旦被加载,可以这样建立一个表格库对象的实例: $this->table
此例演示如何通过一个多维数组(multi-dimensional array)自动生成表格。
注意:数组的第一个索引将成为表头(或者你可以通过set_heading()函数自定义表头)。
$this->load->library('table'); $data = array( array('Name', 'Color', 'Size'), array('Fred', 'Blue', 'Small'), array('Mary', 'Red', 'Large'), array('John', 'Green', 'Medium') ); echo $this->table->generate($data);
效果图:
下面是一个由数据库查询结构创建而成的表格例子。
表格类会基于表格的名字自动地生成表格标题(参考下面记述的函数,你可以使用set_heading()函数设置你自己的标题)。
$this->load->library('table'); $query = $this->db->query("SELECT * FROM my_table"); echo $this->table->generate($query);
效果截图:
$this->load->library('table'); $this->table->set_heading('Name', 'Color', 'Size'); $this->table->add_row('Fred', 'Blue', 'Small'); $this->table->add_row('Mary', 'Red', 'Large'); $this->table->add_row('John', 'Green', 'Medium'); echo $this->table->generate();
$this->load->library('table'); $this->table->set_heading(array('Name', 'Color', 'Size')); $this->table->add_row(array('Fred', 'Blue', 'Small')); $this->table->add_row(array('Mary', 'Red', 'Large')); $this->table->add_row(array('John', 'Green', 'Medium')); echo $this->table->generate();
$tmpl = array ( 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', 'heading_row_start' => '<tr>', 'heading_row_end' => '</tr>', 'heading_cell_start' => '<th>', 'heading_cell_end' => '</th>', 'row_start' => '<tr>', 'row_end' => '</tr>', 'cell_start' => '<td>', 'cell_end' => '</td>', 'row_alt_start' => '<tr>', 'row_alt_end' => '</tr>', 'cell_alt_start' => '<td>', 'cell_alt_end' => '</td>', 'table_close' => '</table>' ); $this->table->set_template($tmpl);
$tmpl = array ( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' ); $this->table->set_template($tmpl);
函数参考:
$this->table->set_caption()
允许你给表格添加一个标题$this->table->set_caption('Colors');
$this->table->set_heading('Name', 'Color', 'Size'); $this->table->set_heading(array('Name', 'Color', 'Size'));
$this->table->add_row()
允许你在你的表格中添加一行。你可以提交一个数组或分开的参数:$this->table->add_row('Blue', 'Red', 'Green'); $this->table->add_row(array('Blue', 'Red', 'Green'));
如果你想要单独设置一个单元格的属性,你可以使用一个关联数组。
关联键名 'data' 定义了这个单元格的数据。
其它的键值对 key => val 将会以 key='val' 的形式被添加为该单元格的属性:
$cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2); $this->table->add_row($cell, 'Red', 'Green'); // 生成 // <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
$this->table->make_columns()
这个函数以一个一维数组为输入,创建一个二维数组,它的深度和列数一样。这个函数可以把一个带有多个元素的单一数组根据表格的列数进行整理并显示。参考下面的例子:$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'); $new_list = $this->table->make_columns($list, 3); $this->table->generate($new_list);生成的HTML代码如下:
<table border="0" cellpadding="4" cellspacing="0"> <tr> <td>one</td><td>two</td><td>three</td> </tr><tr> <td>four</td><td>five</td><td>six</td> </tr><tr> <td>seven</td><td>eight</td><td>nine</td> </tr><tr> <td>ten</td><td>eleven</td><td>twelve</td></tr> </table>
$tmpl = array ( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' ); $this->table->set_template($tmpl); $this->table->set_empty()
$this->table->set_empty(" "); $this->table->clear()
$this->load->library('table'); $this->table->set_heading('Name', 'Color', 'Size'); $this->table->add_row('Fred', 'Blue', 'Small'); $this->table->add_row('Mary', 'Red', 'Large'); $this->table->add_row('John', 'Green', 'Medium'); echo $this->table->generate(); $this->table->clear(); $this->table->set_heading('Name', 'Day', 'Delivery'); $this->table->add_row('Fred', 'Wednesday', 'Express'); $this->table->add_row('Mary', 'Monday', 'Air'); $this->table->add_row('John', 'Saturday', 'Overnight'); echo $this->table->generate(); $this->table->function
$this->load->library('table'); $this->table->set_heading('Name', 'Color', 'Size'); $this->table->add_row('Fred', '<strong>Blue</strong>', 'Small'); $this->table->function = 'htmlspecialchars'; echo $this->table->generate();
<td>Fred</td> <td><strong>Blue</strong></td> <td>Small</td>