日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

html 实现表格控制器,HTML 表格类 - CodeIgniter 2.x 用户手册

發布時間:2024/9/18 HTML 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html 实现表格控制器,HTML 表格类 - CodeIgniter 2.x 用户手册 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

表格類提供了多個函數,允許你從數組或者數據庫結果集中自動生成HTML表格。

初始化類

像CodeIgniter的其它類一樣, 在控制器中使用$this->load->library 函數來初始化表格類:

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

一旦被加載,可以這樣建立一個表格庫對象的實例: $this->table

例子

此例演示如何通過一個多維數組(multi-dimensional array)自動生成表格。

注意:數組的第一個索引將成為表頭(或者你可以通過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();

修改表格的外觀

表格類允許你以你指定的設計編排,去設置表格模板。這里是模板的原型:

$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);

注意:? 在這個模板,你會發現這里有兩個”row”塊設置項。 這是允許你創建隔行顏色,或者設計每行數據的重復間隔元素。

你不必提交全部的模板。如果你只想改變編排的一部分,你可以簡單地提交那部分的元素。在這個例子里,只有表格的開始標簽被更改:

$tmpl = array ( 'table_open'??=> '

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

$this->table->generate()

返回一個包含生成的表格的字符串。 接受一個可選的參數,該參數可以是一個數組或是從數據庫獲取的結果對象。

$this->table->set_caption()

允許你給表格添加一個標題

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

$this->table->set_heading()

允許你設置表格的表頭。你可以提交一個數組或分開的參數:

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

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

$this->table->add_row()

允許你在你的表格中添加一行。你可以提交一個數組或分開的參數:

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

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

如果你想要單獨設置一個單元格的屬性,你可以使用一個關聯數組。關聯鍵名 ‘data’ 定義了這個單元格的數據。其它的鍵值對 key => val 將會以 key=’val’ 的形式被添加為該單元格的屬性:

$cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2);

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

// 生成

//

BlueRedGreen

$this->table->make_columns()

這個函數以一個一維數組為輸入,創建一個二維數組,它的深度和列數一樣。這個函數可以把一個帶有多個元素的單一數組根據表格的列數進行整理并顯示。參考下面的例子:

$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

$this->table->set_template()

允許你設置你的模板。你可以提交整個模板或局部模板。

$tmpl = array ( 'table_open'??=> '

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

$this->table->set_empty()

使你能設置一個默認值,用來顯示在表格中內容為空的單元格。 例如,你可以設置一個non-breaking space(用來防止表格邊框破損的空格):

$this->table->set_empty("?");

$this->table->clear()

使你能清除表格的表頭和行中的數據。如果你需要顯示多個有不同數據的表格,那么你需要在每個表格生成之后調用這個函數來清除之前表格的信息。例如:

$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方法或一個有效的方法應用到所有的單元格中的數據的數組對象。

$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()方法實現html轉義,其結果如下:

Fred<strong>Blue</strong>Small

總結

以上是生活随笔為你收集整理的html 实现表格控制器,HTML 表格类 - CodeIgniter 2.x 用户手册的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。