[PHP]CodeIgniter学习手册(六):HTML表格类

表格类提供了多个函数,允许你从数组或者数据库结果集中自动生成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'          => '<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);


函数参考:


$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');

// 生成

// <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>




$this->table->set_template()
允许你设置你的模板。你可以提交整个模板或局部模板。
$tmpl = array ( 
'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' 
);

$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', '<strong>Blue</strong>', 'Small');

$this->table->function = 'htmlspecialchars';
echo $this->table->generate();

在上面的例子中,所有单元格中的数据都可以通过PHP的htmlspecialchars()方法实现html转义,其结果如下:
<td>Fred</td>

<td><strong>Blue</strong></td>

<td>Small</td>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值