html table class属性,HTML Table Class

HTML Table Class

Table类提供的功能使您能够从数组或数据库结果集中自动生成HTML表格。

使用表类初始化类

例子

改变你的表的外观

类参考

使用表类

初始化类

像CodeIgniter中的大多数其他类一样,Table类在您的控制器中使用以下$this->load->library()方法进行初始化:

$this->load->library('table');

加载后,表格库对象将可用:

$this->table

例子

下面是一个示例,展示如何从多维数组创建表。请注意,第一个数组索引将成为表格标题(或者您可以使用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();

改变你的表的外观

Table类允许您设置一个表格模板,您可以使用该模板指定布局的设计。这是模板原型:

$template = array(

'table_open' => '

'thead_open' => '',

'thead_close' => '

',

'heading_row_start' => '

',

'heading_row_end' => '

',

'heading_cell_start' => '

',

'heading_cell_end' => '

',

'tbody_open' => '

',

'tbody_close' => '

',

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

注意

您会注意到模板中有两组“行”块。这些允许您创建交替的行颜色或与行数据的每次迭代交替的设计元素。

您无需提交完整的模板。如果您只需要更改部分布局,则可以简单地提交这些元素。在这个例子中,只有表格打开标签正在改变:

$template = array(

'table_open' => '

);

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

您也可以在配置文件中为这些设置默认值。

类参考

class CI_Table$function = NULL

允许您指定一个本机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()函数运行,导致:

Fred<strong>Blue</strong>Small

generate([$table_data = NULL])

参数:

$ table_data(mixed) - 用来填充表格行的数据

返回:

HTML表格

返回类型:

$ table_data(mixed) - 用来填充表格行的数据

Returns: HTML table

Return type: string

返回包含生成表的字符串。接受可选参数,该参数可以是数组或数据库结果对象。

set_caption($caption)

参数:

$ caption(字符串) - 表格标题

返回:

CI_Table实例(方法链接)

返回类型:

CI_Table

$ caption(字符串) - 表格标题

Returns: CI\_Table instance (method chaining)

Return type: CI\_Table

允许您为表格添加标题。

$this->table->set_caption('Colors');

set_heading([$args = array()[, ...]])

参数:

$ args(mixed) - 包含表列标题的数组或多个字符串

返回:

CI_Table实例(方法链接)

返回类型:

CI_Table

$ args(mixed) - 包含表列标题的数组或多个字符串

Returns: CI\_Table instance (method chaining)

Return type: CI\_Table

允许您设置表格标题。您可以提交一个数组或离散参数:

$this->table->set_heading('Name', 'Color', 'Size'); $this->table->set_heading(array('Name', 'Color', 'Size'));

add_row([$args = array()[, ...]])

参数:

$ args(mixed) - 包含行值的数组或多个字符串

返回:

CI_Table实例(方法链接)

返回类型:

CI_Table

$ args(mixed) - 包含行值的数组或多个字符串

Returns: CI\_Table instance (method chaining)

Return type: CI\_Table

允许您向表中添加一行。您可以提交一个数组或离散参数:

$this->table->add_row('Blue', 'Red', 'Green'); $this->table->add_row(array('Blue', 'Red', 'Green'));

如果您想要设置单个单元格的标签属性,则可以为该单元格使用关联数组。关联密钥数据定义了单元的数据。任何其他键=> val对都会添加为标签的key ='val'属性:

$cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2); $this->table->add_row($cell, 'Red', 'Green'); // generates //

BlueRedGreen

make_columns([$array = array()[, $col_limit = 0]])

参数:

$ array(array) - 包含多行数据的数组$ col_limit(int) - 表中列的数量

返回:

一个HTML表格列的数组

返回类型:

排列

$ array(array) - 一个包含多行数据的数组

$ col_limit(int) - 表中列的数量

Returns: An array of HTML table columns

Return type: array

This method 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

onetwothree
fourfivesix
seveneightnine
teneleventwelve

set_template($template)

参数:

$ template(array) - 一个包含模板值的关联数组

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

$ template(array) - 一个包含模板值的关联数组

Returns: TRUE on success, FALSE on failure

Return type: bool

Permits you to set your template. You can submit a full or partial template.

$template = array( 'table_open' => '

set_empty($value)

参数:

$ value(混合) - 将值放入空单元格中

返回:

CI_Table实例(方法链接)

返回类型:

CI_Table

$ value(混合) - 将值放入空单元格中

Returns: CI\_Table instance (method chaining)

Return type: CI\_Table

Lets 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(" ");

clear()

返回:

CI_Table实例(方法链接)

返回类型:

CI_Table

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值