?
本文檔使用 php中文網手冊 發(fā)布
Table類提供的功能使您能夠從數組或數據庫結果集中自動生成HTML表格。
使用表類
初始化類
例子
改變你的表的外觀
類參考
像CodeIgniter中的大多數其他類一樣,Table類在您的控制器中使用以下$this->load->library()
方法進行初始化:
$this->load->library('table');
加載后,表格庫對象將可用:
$this->table
下面是一個示例,展示如何從多維數組創(chuàng)建表。請注意,第一個數組索引將成為表格標題(或者您可以使用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);
以下是從數據庫查詢結果創(chuàng)建的表的示例。表類將根據表名稱自動生成標題(或者您可以使用set_heading()
下面的類參考中描述的方法設置您自己的標題)。
$this->load->library('table');$query = $this->db->query('SELECT * FROM my_table');echo $this->table->generate($query);
下面是一個示例,顯示如何使用離散參數創(chuàng)建表格:
$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' => '<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>', '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>');$this->table->set_template($template);
注意
您會注意到模板中有兩組“行”塊。這些允許您創(chuàng)建交替的行顏色或與行數據的每次迭代交替的設計元素。
您無需提交完整的模板。如果您只需要更改部分布局,則可以簡單地提交這些元素。在這個例子中,只有表格打開標簽正在改變:
$template = array( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">');$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', '<strong>Blue</strong>', 'Small');$this->table->function = 'htmlspecialchars';echo $this->table->generate();
在上面的例子中,所有單元格數據都將通過PHP htmlspecialchars()
函數運行,導致:
<td>Fred</td><td><strong>Blue</strong></td><td>Small</td>
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 // <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
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 <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>
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' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' ); $this->table->set_template($template);
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 |