HTML 表格類
表格類提供了多個函數(shù),允許你從數(shù)組或者數(shù)據(jù)庫結(jié)果集中自動生成HTML表格。
初始化類
像CodeIgniter的其它類一樣, 在控制器中使用$this->load->library 函數(shù)來初始化表格類:
$this->load->library('table');
一旦被加載,可以這樣建立一個表格庫對象的實例: $this->table
例子
此例演示如何通過一個多維數(shù)組(multi-dimensional array)自動生成表格。 注意:數(shù)組的第一個索引將成為表頭(或者你可以通過set_heading()函數(shù)自定義表頭)。
$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);
這里是一個由數(shù)據(jù)庫查詢結(jié)構(gòu)創(chuàng)建而成的表格例子。表格類會基于表格的名字自動地生成表格標(biāo)題(參考下面記述的函數(shù),你可以使用set_heading()函數(shù)設(shè)置你自己的標(biāo)題)。
$this->load->library('table');
$query = $this->db->query("SELECT * FROM my_table");
echo $this->table->generate($query);
此例演示了如何使用連續(xù)的參數(shù)創(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();
這個簡單的例子,除了更換個別的參數(shù)外,還使用了數(shù)組:
$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();
修改表格的外觀
表格類允許你以你指定的設(shè)計編排,去設(shè)置表格模板。這里是模板的原型:
$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);
注意:? 在這個模板,你會發(fā)現(xiàn)這里有兩個"row"塊設(shè)置項。 這是允許你創(chuàng)建隔行顏色,或者設(shè)計每行數(shù)據(jù)的重復(fù)間隔元素。
你不必提交全部的模板。如果你只想改變編排的一部分,你可以簡單地提交那部分的元素。在這個例子里,只有表格的開始標(biāo)簽被更改:
$tmpl = array ( 'table_open'??=> '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );
$this->table->set_template($tmpl);
函數(shù)參考
$this->table->generate()
返回一個包含生成的表格的字符串。 接受一個可選的參數(shù),該參數(shù)可以是一個數(shù)組或是從數(shù)據(jù)庫獲取的結(jié)果對象。
$this->table->set_caption()
允許你給表格添加一個標(biāo)題
$this->table->set_caption('Colors');
$this->table->set_heading()
允許你設(shè)置表格的表頭。你可以提交一個數(shù)組或分開的參數(shù):
$this->table->set_heading('Name', 'Color', 'Size');
$this->table->set_heading(array('Name', 'Color', 'Size'));
$this->table->add_row()
允許你在你的表格中添加一行。你可以提交一個數(shù)組或分開的參數(shù):
$this->table->add_row('Blue', 'Red', 'Green');
$this->table->add_row(array('Blue', 'Red', 'Green'));
如果你想要單獨設(shè)置一個單元格的屬性,你可以使用一個關(guān)聯(lián)數(shù)組。關(guān)聯(lián)鍵名 'data' 定義了這個單元格的數(shù)據(jù)。其它的鍵值對 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()
這個函數(shù)以一個一維數(shù)組為輸入,創(chuàng)建一個二維數(shù)組,它的深度和列數(shù)一樣。這個函數(shù)可以把一個帶有多個元素的單一數(shù)組根據(jù)表格的列數(shù)進行整理并顯示。參考下面的例子:
$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()
允許你設(shè)置你的模板。你可以提交整個模板或局部模板。
$tmpl = array ( 'table_open'??=> '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );
$this->table->set_template($tmpl);
$this->table->set_empty()
使你能設(shè)置一個默認值,用來顯示在表格中內(nèi)容為空的單元格。 例如,你可以設(shè)置一個non-breaking space(用來防止表格邊框破損的空格):
$this->table->set_empty(" ");
$this->table->clear()
使你能清除表格的表頭和行中的數(shù)據(jù)。如果你需要顯示多個有不同數(shù)據(jù)的表格,那么你需要在每個表格生成之后調(diào)用這個函數(shù)來清除之前表格的信息。例如:
$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方法或一個有效的方法應(yīng)用到所有的單元格中的數(shù)據(jù)的數(shù)組對象。
$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();
在上面的例子中,所有單元格中的數(shù)據(jù)都可以通過PHP的htmlspecialchars()方法實現(xiàn)html轉(zhuǎn)義,其結(jié)果如下:
<td>Fred</td><td><strong>Blue</strong></td><td>Small</td>
?