CodeIgniter 使用手冊版本 2.1.4


HTML Table 類別

HTML Table類別賦與你能從陣列或是資料庫的回傳結果自動產生表格

初始化類別

就像大多數在CI中的類別一樣,在控制器中初始化 HTML Table 類別使用 $this->load->library 函數:

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

載入之後,HTML Table類別可以這樣使用: $this->table

範例

這個範例將告訴你如何以一個多維的陣列來建出一個表格內的欄位,陣列的第一個索引將會成為表格的標題(或是你可以在之後使用 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();

改變表格的外觀

HTML Table 類別允許你使用指定的樣版,底下的範例是一個樣版的模型

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

注意:  這個樣版擁有二個 "row" 的區塊,可以讓你在陳列資料時使用顏色交替互換

如果你只是要改變樣版的部份樣式時,你可以簡單的提供幾個元素,而不需要提供整個樣版:

$tmpl = array ( 'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );

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

函數參考

$this->table->generate()

回傳所建立的 Table

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

如果你想要設定個別元素的屬性,你可以使用關聯式陣列,在關聯式陣列中索引代表元素名稱,而值則是你想要設定的屬性:

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

$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

<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()

指定樣版,你可以提供完整或是部份的樣版

$tmpl = array ( 'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );

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

$this->table->set_empty()

為空白的欄位設定一個預設值:

$this->table->set_empty("&nbsp;");

$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', '<strong>Blue</strong>', 'Small');

$this->table->function = 'htmlspecialchars';
echo $this->table->generate();

在上述的範例中,各個欄位的內容都經過 htmlspecialchars() 函數的轉換:

<td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td>