表类提供了多个函数,允许你从数组或者数据库结果集中自动生成html表。 初始化类 像codeigniter的其它类一样, 在控制器中使用$this-load-library 函数来初始化表类: $this-load-library('table'); 一旦被加载,可以这样建立一个表库对象的实例: $this-table
表格类提供了多个函数,允许你从数组或者数据库结果集中自动生成html表格。
初始化类
像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' => '
', 'heading_row_start' => '', 'heading_row_end' => '
', 'heading_cell_start' => '', 'heading_cell_end' => '', 'row_start' => '', 'row_end' => '
', 'cell_start' => '', 'cell_end' => ' ', 'row_alt_start' => '', 'row_alt_end' => '
', 'cell_alt_start' => '', 'cell_alt_end' => ' ', 'table_close' => '
' );$this->table->set_template($tmpl);
注意: 在这个模板,你会发现这里有两个row块设置项。 这是允许你创建隔行颜色,或者设计每行数据的重复间隔元素。
$tmpl = array ( 'table_open' => '
' );$this->table->set_template($tmpl);
函数参考:
$this->table->generate()
返回一个包含生成的表格的字符串。 接受一个可选的参数,该参数可以是一个数组或是从数据库获取的结果对象。
$this->table->set_caption()
允许你给表格添加一个标题
$this->table->set_caption('colors');
$this->table->set_heading()
允许你设置表格的表头。你可以提交一个数组或分开的参数:
$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');// 生成// blue red green
$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代码如下:
one two three
four five six
seven eight nine
ten eleven twelve
$this->table->set_template()
允许你设置你的模板。你可以提交整个模板或局部模板。
$tmpl = array ( 'table_open' => '
' );$this->table->set_template($tmpl);$this->table->set_empty()
使你能设置一个默认值,用来显示在表格中内容为空的单元格。 例如,你可以设置一个non-breaking space(用来防止表格边框破损的空格):
$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
允许你指定一个本地的php方法或一个有效的方法应用到所有的单元格中的数据的数组对象。
$this->load->library('table');$this->table->set_heading('name', 'color', 'size');$this->table->add_row('fred', 'blue', 'small');$this->table->function = 'htmlspecialchars';echo $this->table->generate();
在上面的例子中,所有单元格中的数据都可以通过php的htmlspecialchars()方法实现html转义,其结果如下:
fred blue small