html 实现表格控制器,HTML 表格类 - CodeIgniter 2.x 用户手册
表格類提供了多個(gè)函數(shù),允許你從數(shù)組或者數(shù)據(jù)庫(kù)結(jié)果集中自動(dòng)生成HTML表格。
初始化類
像CodeIgniter的其它類一樣, 在控制器中使用$this->load->library 函數(shù)來初始化表格類:
$this->load->library('table');
一旦被加載,可以這樣建立一個(gè)表格庫(kù)對(duì)象的實(shí)例: $this->table
例子
此例演示如何通過一個(gè)多維數(shù)組(multi-dimensional array)自動(dòng)生成表格。
注意:數(shù)組的第一個(gè)索引將成為表頭(或者你可以通過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);
這里是一個(gè)由數(shù)據(jù)庫(kù)查詢結(jié)構(gòu)創(chuàng)建而成的表格例子。表格類會(huì)基于表格的名字自動(dò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)建一個(gè)表格:
$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();
這個(gè)簡(jiǎn)單的例子,除了更換個(gè)別的參數(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è)計(jì)編排,去設(shè)置表格模板。這里是模板的原型:
$tmpl = array (
'table_open'??????????=> '
'heading_row_start'???=> '
','heading_row_end'?????=> '
','heading_cell_start'??=> '
','heading_cell_end'????=> '
','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($tmpl);
注意:? 在這個(gè)模板,你會(huì)發(fā)現(xiàn)這里有兩個(gè)”row”塊設(shè)置項(xiàng)。 這是允許你創(chuàng)建隔行顏色,或者設(shè)計(jì)每行數(shù)據(jù)的重復(fù)間隔元素。
你不必提交全部的模板。如果你只想改變編排的一部分,你可以簡(jiǎn)單地提交那部分的元素。在這個(gè)例子里,只有表格的開始標(biāo)簽被更改:
$tmpl = array ( 'table_open'??=> '
$this->table->set_template($tmpl);
$this->table->generate()
返回一個(gè)包含生成的表格的字符串。 接受一個(gè)可選的參數(shù),該參數(shù)可以是一個(gè)數(shù)組或是從數(shù)據(jù)庫(kù)獲取的結(jié)果對(duì)象。
$this->table->set_caption()
允許你給表格添加一個(gè)標(biāo)題
$this->table->set_caption('Colors');
$this->table->set_heading()
允許你設(shè)置表格的表頭。你可以提交一個(gè)數(shù)組或分開的參數(shù):
$this->table->set_heading('Name', 'Color', 'Size');
$this->table->set_heading(array('Name', 'Color', 'Size'));
$this->table->add_row()
允許你在你的表格中添加一行。你可以提交一個(gè)數(shù)組或分開的參數(shù):
$this->table->add_row('Blue', 'Red', 'Green');
$this->table->add_row(array('Blue', 'Red', 'Green'));
如果你想要單獨(dú)設(shè)置一個(gè)單元格的屬性,你可以使用一個(gè)關(guān)聯(lián)數(shù)組。關(guān)聯(lián)鍵名 ‘data’ 定義了這個(gè)單元格的數(shù)據(jù)。其它的鍵值對(duì) key => val 將會(huì)以 key=’val’ 的形式被添加為該單元格的屬性:
$cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2);
$this->table->add_row($cell, 'Red', 'Green');
// 生成
//
BlueRedGreen$this->table->make_columns()
這個(gè)函數(shù)以一個(gè)一維數(shù)組為輸入,創(chuàng)建一個(gè)二維數(shù)組,它的深度和列數(shù)一樣。這個(gè)函數(shù)可以把一個(gè)帶有多個(gè)元素的單一數(shù)組根據(jù)表格的列數(shù)進(jìn)行整理并顯示。參考下面的例子:
$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
| one | two | three |
| four | five | six |
| seven | eight | nine |
| ten | eleven | twelve |
$this->table->set_template()
允許你設(shè)置你的模板。你可以提交整個(gè)模板或局部模板。
$tmpl = array ( 'table_open'??=> '
$this->table->set_template($tmpl);
$this->table->set_empty()
使你能設(shè)置一個(gè)默認(rèn)值,用來顯示在表格中內(nèi)容為空的單元格。 例如,你可以設(shè)置一個(gè)non-breaking space(用來防止表格邊框破損的空格):
$this->table->set_empty("?");
$this->table->clear()
使你能清除表格的表頭和行中的數(shù)據(jù)。如果你需要顯示多個(gè)有不同數(shù)據(jù)的表格,那么你需要在每個(gè)表格生成之后調(diào)用這個(gè)函數(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
允許你指定一個(gè)本地的PHP方法或一個(gè)有效的方法應(yīng)用到所有的單元格中的數(shù)據(jù)的數(shù)組對(duì)象。
$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();
在上面的例子中,所有單元格中的數(shù)據(jù)都可以通過PHP的htmlspecialchars()方法實(shí)現(xiàn)html轉(zhuǎn)義,其結(jié)果如下:
Fred<strong>Blue</strong>Small總結(jié)
以上是生活随笔為你收集整理的html 实现表格控制器,HTML 表格类 - CodeIgniter 2.x 用户手册的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云计算比本地计算机可靠,1-云计算复习题
- 下一篇: abap alv新增行数据_ALV DM