HTML 表格类

Table 类提供了使你能够从数组或数据库结果集自动生成 HTML 表格的方法。

使用 Table 类

初始化类

Table 类没有作为服务提供,应该进行"正常"实例化,例如:

<?php
$table = new \CodeIgniter\View\Table();

例子

下面是一个示例,展示了如何从多维数组创建表格。请注意,第一个数组索引将成为表格标题(或者你可以使用下面函数参考中描述的 setHeading() 方法设置自己的标题)。

<?php
$table = new \CodeIgniter\View\Table();
$data = [
 ['Name', 'Color', 'Size'],
 ['Fred', 'Blue', 'Small'],
 ['Mary', 'Red', 'Large'],
 ['John', 'Green', 'Medium'],
];
echo $table->generate($data);

下面是从数据库查询结果创建的表格示例。表类将自动基于表名生成标题(或者你可以使用下面类参考中描述的 setHeading() 方法设置自己的标题)。

<?php
$table = new \CodeIgniter\View\Table();
$query = $db->query('SELECT * FROM my_table');
echo $table->generate($query);

下面是一个使用离散参数创建表格的示例:

<?php
$table = new \CodeIgniter\View\Table();
$table->setHeading('Name', 'Color', 'Size');
$table->addRow('Fred', 'Blue', 'Small');
$table->addRow('Mary', 'Red', 'Large');
$table->addRow('John', 'Green', 'Medium');
echo $table->generate();

下面是相同的示例,只是使用数组代替各个参数:

<?php
$table = new \CodeIgniter\View\Table();
$table->setHeading(['Name', 'Color', 'Size']);
$table->addRow(['Fred', 'Blue', 'Small']);
$table->addRow(['Mary', 'Red', 'Large']);
$table->addRow(['John', 'Green', 'Medium']);
echo $table->generate();

更改表格外观

Table 类允许你设置一个表格模板来指定布局设计。下面是模板原型:

<?php
$template = [
 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
 'thead_open' => '<thead>',
 'thead_close' => '</thead>',
 'heading_row_start' => '<tr>',
 'heading_row_end' => '</tr>',
 'heading_cell_start' => '<th>',
 'heading_cell_end' => '</th>',
 'tfoot_open' => '<tfoot>',
 'tfoot_close' => '</tfoot>',
 'footing_row_start' => '<tr>',
 'footing_row_end' => '</tr>',
 'footing_cell_start' => '<td>',
 'footing_cell_end' => '</td>',
 'tbody_open' => '<tbody>',
 'tbody_close' => '</tbody>',
 '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>',
];
$table->setTemplate($template);

备注

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

你不需要提交完整的模板。如果你只需要更改布局的一部分,只需提交这些元素即可。在此示例中,仅更改表格打开标签:

<?php
$template = [
 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">',
];
$table->setTemplate($template);

你还可以通过向 Table 构造函数传递模板设置数组来为这些设置默认值:

<?php
$customSettings = [
 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">',
];
$table = new \CodeIgniter\View\Table($customSettings);

同步行与标题

在 4.4.0 版本加入.

setSyncRowsWithHeading(true) 方法使得每个数据值都放置在与 setHeading() 中定义的相同列中,如果参数使用了关联数组。这在处理通过 REST API 加载的数据时特别有用,因为其顺序可能不符合你的要求,或者如果 API 返回了过多的数据。

如果数据行包含一个在标题中不存在的键,则其值将被过滤。相反,如果数据行中没有列在标题中列出的键,则会在其位置放置一个空单元格。

<?php
$table = new \CodeIgniter\View\Table();
$table->setHeading(['name' => 'Name', 'color' => 'Color', 'size' => 'Size'])
 ->setSyncRowsWithHeading(true)
 ->addRow(['color' => 'Blue', 'name' => 'Fred', 'size' => 'Small'])
 ->addRow(['size' => 'Large', 'age' => '24', 'name' => 'Mary'])
 ->addRow(['color' => 'Green']);
echo $table->generate();
?>
<!-- Generates a table with this prototype: -->
<table border="0" cellpadding="4" cellspacing="0">
 <thead>
 <tr>
 <th>Name</th>
 <th>Color</th>
 <th>Size</th>
 </tr>
 </thead>
 <tbody>
 <tr>
 <td>Fred</td>
 <td>Blue</td>
 <td>Small</td>
 </tr>
 <tr>
 <td>Mary</td>
 <td></td>
 <td>Large</td>
 </tr>
 <tr>
 <td></td>
 <td>Green</td>
 <td></td>
 </tr>
 </tbody>
</table>

重要

你必须在通过 addRow([...]) 添加任何行之前调用 setSyncRowsWithHeading(true)setHeading([...]),以进行列的重新排列。

使用数组作为 generate() 的输入会产生相同的结果:

<?php
$data = [
 [
 'color' => 'Blue',
 'name' => 'Fred',
 'size' => 'Small',
 ],
 [
 'size' => 'Large',
 'age' => '24',
 'name' => 'Mary',
 ],
 [
 'color' => 'Green',
 ],
];
$table = new \CodeIgniter\View\Table();
$table->setHeading(['name' => 'Name', 'color' => 'Color', 'size' => 'Size'])
 ->setSyncRowsWithHeading(true);
echo $table->generate($data);

类参考

class CodeIgniter\View\Table
$function = null

允许你指定 native PHP 函数或一个有效的函数数组对象应用于所有单元格数据。

<?php
$table = new \CodeIgniter\View\Table();
$table->setHeading('Name', 'Color', 'Size');
$table->addRow('Fred', '<strong>Blue</strong>', 'Small');
$table->function = 'htmlspecialchars';
echo $table->generate();

在上面的例子中,所有单元格数据都将通过 PHP 的 htmlspecialchars() 函数运行,结果是:

<td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td>
generate([$tableData = null])
参数:
  • $tableData (mixed) – 用来填充表格行的数据

返回:

HTML表格

返回类型:

string

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

setCaption($caption)
参数:
  • $caption (string) – 表格标题

返回:

Table 实例(方法链式调用)

返回类型:

Table

允许你为表格添加标题。

<?php
$table->setCaption('Colors');
setHeading([$args = [][, ...]])
参数:
  • $args (mixed) – 包含表格列标题的数组或多个字符串

返回:

Table 实例(方法链式调用)

返回类型:

Table

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

<?php
$table->setHeading('Name', 'Color', 'Size'); // or
$table->setHeading(['Name', 'Color', 'Size']);
setFooting([$args = [][, ...]])
参数:
  • $args (mixed) – 包含表格页脚值的数组或多个字符串

返回:

Table 实例(方法链式调用)

返回类型:

Table

允许你设置表格页脚。你可以提交数组或离散参数:

<?php
$table->setFooting('Subtotal', $subtotal, $notes); // or
$table->setFooting(['Subtotal', $subtotal, $notes]);
addRow([$args = [][, ...]])
参数:
  • $args (mixed) – 包含行值的数组或多个字符串

返回:

Table 实例(方法链式调用)

返回类型:

Table

允许你向表格添加行。你可以提交数组或离散参数:

<?php
$table->addRow('Blue', 'Red', 'Green'); // or
$table->addRow(['Blue', 'Red', 'Green']);

如果你想为单个单元格的标签属性设置值,可以为该单元格使用关联数组。 关联键 data 定义单元格的数据。任何其他的 key => val 对会作为 key=’val’ 属性添加到标签中:

<?php
$cell = ['data' => 'Blue', 'class' => 'highlight', 'colspan' => 2];
$table->addRow($cell, 'Red', 'Green');
?>
<!-- Generates: -->
<td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
makeColumns([$array = [][, $columnLimit = 0]])
参数:
  • $array (array) – 包含多个行数据的数组

  • $columnLimit (int) – 表格中的列数

返回:

HTML表格列的多维数组

返回类型:

array

此方法接受一维数组作为输入,并创建深度等于所需列数的多维数组。 这允许具有大量元素的单个数组以固定列数显示在表格中。考虑这个例子:

<?php
$list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'];
$newList = $table->makeColumns($list, 3);
$table->generate($newList);
?>
<!-- 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>
setTemplate($template)
参数:
  • $template (array) – 包含模板值的关联数组

返回:

成功为 true,失败为 false

返回类型:

bool

允许你设置模板。你可以提交完整或部分模板。

<?php
$template = [
 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">',
];
$table->setTemplate($template);
setEmpty($value)
参数:
  • $value (mixed) – 放入空单元格中的值

返回:

Table 实例(方法链式调用)

返回类型:

Table

允许你为使用在任何空表格单元格中的默认值设置值。 例如,你可以设置一个不间断的空格:

<?php
$table->setEmpty('&nbsp;');
clear()
返回:

Table 实例(方法链式调用)

返回类型:

Table

允许你清除表格标题、行数据和标题。 如果你需要显示包含不同数据的多个表格,你应该在每个表格生成后调用此方法,以清除之前的表格信息。

例子

<?php
$table = new \CodeIgniter\View\Table();
$table->setCaption('Preferences')
 ->setHeading('Name', 'Color', 'Size')
 ->addRow('Fred', 'Blue', 'Small')
 ->addRow('Mary', 'Red', 'Large')
 ->addRow('John', 'Green', 'Medium');
echo $table->generate();
$table->clear();
$table->setCaption('Shipping')
 ->setHeading('Name', 'Day', 'Delivery')
 ->addRow('Fred', 'Wednesday', 'Express')
 ->addRow('Mary', 'Monday', 'Air')
 ->addRow('John', 'Saturday', 'Overnight');
echo $table->generate();
setSyncRowsWithHeading(bool $orderByKey)
返回:

Table 实例(方法链式调用)

返回类型:

Table

启用每个行数据键按照标题键进行排序。这样可以更好地控制数据在正确列中的显示位置。确保在调用第一个 addRow() 方法之前设置此值。