CodeIgniter4 HTML Table 類

2020-08-17 15:46 更新

Table類提供了使您能夠從數(shù)組或數(shù)據(jù)庫結(jié)果集中自動生成HTML表的方法。

使用Table類

初始化類

Table類未作為服務(wù)提供,應(yīng)“正?!睂?shí)例化,例如:

$table = new \CodeIgniter\View\Table();

例子

這是顯示如何從多維數(shù)組創(chuàng)建表的示例。請注意,第一個數(shù)組索引將成為表標(biāo)題(或者您可以使用setHeading() 下面的函數(shù)參考中描述的方法來設(shè)置自己的標(biāo)題)。

$table = new \CodeIgniter\View\Table();


$data = [
        ['Name', 'Color', 'Size'],
        ['Fred', 'Blue',  'Small'],
        ['Mary', 'Red',   'Large'],
        ['John', 'Green', 'Medium'],
];


echo $table->generate($data);

這是根據(jù)數(shù)據(jù)庫查詢結(jié)果創(chuàng)建的表的示例。表格類將根據(jù)表格名稱自動生成標(biāo)題(或者您可以使用setHeading() 下面的類參考中描述的方法來設(shè)置自己的標(biāo)題)。

$table = new \CodeIgniter\View\Table();


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


echo $table->generate($query);

這是顯示如何使用離散參數(shù)創(chuàng)建表的示例:

$table = new \CodeIgniter\View\Table();


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


$table->addRow('Fred', 'Blue', 'Small');
$table->addRow('Mary', 'Red', 'Large');
$table->addRow('John', 'Green', 'Medium');


echo $table->generate();

這是相同的示例,除了使用單個數(shù)組代替單個參數(shù)外:

$table = new \CodeIgniter\View\Table();


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


$table->addRow(['Fred', 'Blue', 'Small']);
$table->addRow(['Mary', 'Red', 'Large']);
$table->addRow(['John', 'Green', 'Medium']);


echo $table->generate();

改變Table的外觀

Table類允許您設(shè)置表格模板,通過該模板可以指定布局的設(shè)計(jì)。這是模板原型:

$template = [
        '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>',


        'tfoot_open'         => '<tfoot>',
        'tfoot_close'        => '</tfoot>',


        'footing_row_start'  => '<tr>',
        'footing_row_end'    => '</tr>',
        'footing_cell_start' => '<td>',
        'footing_cell_end'   => '</td>',


        '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>'
];


$table->setTemplate($template);

注解

您會注意到模板中有兩組“行”塊。這些允許您創(chuàng)建交替的行顏色或與行數(shù)據(jù)的每次迭代交替的設(shè)計(jì)元素。

您無需提交完整的模板。如果只需要更改布局的某些部分,則只需提交這些元素。在此示例中,僅更改了表開始標(biāo)記:

$template = [
        'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
];


$table->setTemplate($template);

您還可以通過將模板設(shè)置數(shù)組傳遞給Table構(gòu)造函數(shù)來為這些設(shè)置默認(rèn)值:

$customSettings = [
        'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
];


$table = new \CodeIgniter\View\Table($customSettings);

類參考

classTable

$function = NULL

允許您指定要應(yīng)用于所有單元格數(shù)據(jù)的本地PHP函數(shù)或有效函數(shù)數(shù)組對象。

$table = new \CodeIgniter\View\Table();


$table->setHeading('Name', 'Color', 'Size');
$table->addRow('Fred', '<strong>Blue</strong>', 'Small');


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

在上面的示例中,所有單元格數(shù)據(jù)都將通過PHP的htmlspecialchars()函數(shù)運(yùn)行,結(jié)果是:

<td>Fred</td><td><strong>Blue</strong></td><td>Small</td>

generate([$ tableData = NULL])

參數(shù): $ tableDatamixed)–用表填充行的數(shù)據(jù)
返回: HTML表格
返回類型: string

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

setCaption$ caption

參數(shù): $ captionstring)–表標(biāo)題
返回: 表實(shí)例(方法鏈接)
返回類型: Table

允許您向表格添加標(biāo)題。

$table->setCaption('Colors');

setHeading([$ args = [][, ...]])

參數(shù): $ argsmixed)–包含表列標(biāo)題的數(shù)組或多個字符串
返回: 表實(shí)例(方法鏈接)
返回類型: Table

允許您設(shè)置表格標(biāo)題。您可以提交數(shù)組或離散參數(shù):

$table->setHeading('Name', 'Color', 'Size'); // or


$table->setHeading(['Name', 'Color', 'Size']);

setFooting([$ args = [][, ...]])

參數(shù): $ argsmixed)–包含表基礎(chǔ)值的數(shù)組或多個字符串
返回: 表實(shí)例(方法鏈接)
返回類型: Table

允許您設(shè)置表格基礎(chǔ)。您可以提交數(shù)組或離散參數(shù):

$table->setFooting('Subtotal', $subtotal, $notes); // or


$table->setFooting(['Subtotal', $subtotal, $notes]);

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

參數(shù): $ argsmixed)–包含行值的數(shù)組或多個字符串
返回: 表實(shí)例(方法鏈接)
返回類型: Table

允許您在表格中添加一行。您可以提交數(shù)組或離散參數(shù):

$table->addRow('Blue', 'Red', 'Green'); // or


$table->addRow(['Blue', 'Red', 'Green']);

如果要設(shè)置單個單元格的標(biāo)簽屬性,則可以對該單元格使用關(guān)聯(lián)數(shù)組。關(guān)聯(lián)密鑰數(shù)據(jù)定義單元格的數(shù)據(jù)。其他任何key => val對將作為key ='val'屬性添加到標(biāo)簽:

$cell = ['data' => 'Blue', 'class' => 'highlight', 'colspan' => 2];
$table->addRow($cell, 'Red', 'Green');


// generates
// <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>

makeColumns([$ array = [][,$ columnLimit = 0]])

參數(shù): $ arrayarray)–包含多行數(shù)據(jù)的數(shù)組
$ columnLimitint)–表中的列數(shù)
返回: HTML表列的數(shù)組
返回類型: array

此方法將一維數(shù)組作為輸入,并創(chuàng)建深度等于所需列數(shù)的多維數(shù)組。這允許具有多個元素的單個數(shù)組顯示在具有固定列數(shù)的表中??紤]以下示例:

$list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'];


$newList = $table->makeColumns($list, 3);


$table->generate($newList);


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

setTemplate$ template

參數(shù): $ templatearray)–包含模板值的關(guān)聯(lián)數(shù)組
返回: 成功則為TRUE,失敗則為FALSE
返回類型: bool

允許您設(shè)置模板。您可以提交完整或部分模板。

$template = [
        'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
];


$table->setTemplate($template);

setEmpty$ value

參數(shù): $ value混合)–放入空單元格的值
返回: 表實(shí)例(方法鏈接)
返回類型: Table

使您可以設(shè)置一個默認(rèn)值,以用于任何空的表單元格。例如,您可以設(shè)置一個不間斷的空格:

$table->setEmpty(" ");

clear()

返回: 表實(shí)例(方法鏈接)
返回類型: Table

讓您清除表標(biāo)題,行數(shù)據(jù)和標(biāo)題。如果需要顯示具有不同數(shù)據(jù)的多個表,則應(yīng)在生成每個表后清除以前的表信息,然后調(diào)用此方法。

$table = new \CodeIgniter\View\Table();




$table->setCaption('Preferences')
    ->setHeading('Name', 'Color', 'Size')
    ->addRow('Fred', 'Blue', 'Small')
    ->addRow('Mary', 'Red', 'Large')
    ->addRow('John', 'Green', 'Medium');


echo $table->generate();


$table->clear();


$table->setCaption('Shipping')
    ->setHeading('Name', 'Day', 'Delivery')
    ->addRow('Fred', 'Wednesday', 'Express')
    ->addRow('Mary', 'Monday', 'Air')
    ->addRow('John', 'Saturday', 'Overnight');


echo $table->generate();
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號