CodeIgniter HTML 表格類

2018-07-21 15:40 更新

HTML 表格類

表格類提供了一些方法用于根據(jù)數(shù)組或數(shù)據(jù)庫結(jié)果集自動生成 HTML 的表格。

使用表格類

初始化類

跟 CodeIgniter 中的其他類一樣,可以在你的控制器中使用 $this->load->library() 方法加載表格類:

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

一旦加載,表格類就可以像下面這樣使用:

$this->table

例子

下面這個例子向你演示了如何通過多維數(shù)組來創(chuàng)建一個表格。注意數(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é)果來創(chuàng)建一個表格。表格類將使用查詢結(jié)果的列名自動生成表頭 (或者你也可以通過下面介紹的 set_heading() 方法來設(shè)置你自己的表頭)。

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

$query = $this->db->query('SELECT * FROM my_table');

echo $this->table->generate($query);

下面這個例子演示了如何使用分開的參數(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();

下面這個例子和上面的一樣,但是它不是使用分開的參數(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è)計表格的樣式,下面是模板的原型:

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

注解

你會發(fā)現(xiàn)模板中有兩個 "row" 代碼塊,它可以讓你的表格每行使用交替的顏色, 或者其他的這種隔行的設(shè)計元素。

你不用設(shè)置整個模板,只需要設(shè)置你想修改的部分即可。在下面這個例子中,只有 table 的起始標(biāo)簽需要修改:

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

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

你也可以在配置文件中設(shè)置默認(rèn)的模板。

類參考

classCI_Table

$function = NULL

允許你指定一個原生的 PHP 函數(shù)或一個有效的函數(shù)數(shù)組對象,該函數(shù)會作用于所有的單元格數(shù)據(jù)。

$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() 函數(shù),結(jié)果如下:

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

generate([$table_data = NULL])

參數(shù):

  • $table_data (mixed) -- Data to populate the table rows with

返回: HTML table

返回類型: string

返回生成的表格的字符串。 接受一個可選的參數(shù),該參數(shù)可以是一個數(shù)組或是從數(shù)據(jù)庫獲取的結(jié)果對象。

set_caption($caption)

參數(shù):

  • $caption (string) -- Table caption

返回: CI_Table instance (method chaining)

返回類型: CI_Table

允許你給表格添加一個標(biāo)題。

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

set_heading([$args = array()[, ...]])

參數(shù):

  • $args (mixed) -- An array or multiple strings containing the table column titles

返回: CI_Table instance (method chaining)

返回類型: CI_Table

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

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

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

add_row([$args = array()[, ...]])

參數(shù):

  • $args (mixed) -- An array or multiple strings containing the row values

返回: CI_Table instance (method chaining)

返回類型: CI_Table

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

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

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

如果你想要單獨(dú)設(shè)置一個單元格的屬性,你可以使用一個關(guān)聯(lián)數(shù)組。關(guān)聯(lián)數(shù)組的鍵名 data 定義了這個單元格的數(shù)據(jù)。 其它的鍵值對 key => val 將會以 key='val' 的形式被添加為該單元格的屬性里:

$cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2); $this->table->add_row($cell, 'Red', 'Green');

// generates // BlueRedGreen

make_columns([$array = array()[, $col_limit = 0]])

參數(shù):

  • $array (array) -- An array containing multiple rows' data
  • $col_limit (int) -- Count of columns in the table

返回: An array of HTML table columns

返回類型: array

這個函數(shù)以一個一維數(shù)組為輸入,創(chuàng)建一個多維數(shù)組,它的深度(譯注:不是行數(shù),而是每一行的元素個數(shù))和列數(shù)一樣。 這個函數(shù)可以把一個含有多個元素的數(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

onetwothree fourfivesix seveneightnine teneleventwelve

set_template($template)

參數(shù):

  • $template (array) -- An associative array containing template values

返回: TRUE on success, FALSE on failure

返回類型: bool

允許你設(shè)置你的模板。你可以提交整個模板或部分模板。

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

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

set_empty($value)

參數(shù):

  • $value (mixed) -- Value to put in empty cells

返回:CI_Table instance (method chaining)

返回類型: CI_Table

用于設(shè)置當(dāng)表格中的單元格為空時要顯示的默認(rèn)值。例如,設(shè)置一個不換行空格(NBSP,non-breaking space):

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

clear()

返回: CI_Table instance (method chaining)
返回類型: CI_Table

使你能清除表格的表頭和行中的數(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();

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號