HTML 表格类
表格类提供允许你从数组或数据库查询结果集中自动生成HTML表格
初始化类
像CodeIgniter的其它类一样, 表格类在控制器中使用$this->load->library 函数进行初始化:
$this->load->library('table');
一旦被加载,可以这样建立一个表格库对象的实例: $this->table
例子
此例演示如何通过一个多维数组(multi-dimensional array)自动生成表格。 注意:数组的第一个索引将成为表头(或者你可以通过 set_heading() function 函数自定义表头)。
$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);
注意: 在这个模板,你会发现这里有两个"row"块设置项。 这是允许你创建隔行颜色,或者设计每行数据的重复间隔元素。
你不必提交全部的模板。如果你只想改变编排的一部分,你可以简单地提交那部分的元素。在这个例子里,只有表格的开始标签被更改:
$tmpl = array ( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );
$this->table->set_template($tmpl);
Function Reference
$this->table->generate()
Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object.
$this->table->set_caption()
Permits you to add a caption to the table.
$this->table->set_caption('Colors');
$this->table->set_heading()
Permits you to set the table heading. You can submit an array or discrete params:
$this->table->set_heading('Name', 'Color', 'Size');
$this->table->set_heading(array('Name', 'Color', 'Size'));
$this->table->add_row()
Permits you to add a row to your table. You can submit an array or discrete params:
$this->table->add_row('Blue', 'Red', 'Green');
$this->table->add_row(array('Blue', 'Red', 'Green'));
$this->table->make_columns()
This function takes a one-dimensional array as input and creates a multi-dimensional array with a depth equal to the number of columns desired. This allows a single array with many elements to be displayed in a table that has a fixed column count. Consider this example:
$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);
// Generates a table with this prototype
<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>
$this->table->set_template()
Permits you to set your template. You can submit a full or partial template.
$tmpl = array ( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );
$this->table->set_template($tmpl);
$this->table->set_empty()
Let's you set a default value for use in any table cells that are empty. You might, for example, set a non-breaking space:
$this->table->set_empty(" ");
$this->table->clear()
Lets you clear the table heading and row data. If you need to show multiple tables with different data you should to call this function after each table has been generated to empty the previous table information. Example:
$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();