bootpress/table

创建易于可视化和查看内容的HTML表格。

v1.0 2016-10-19 16:55 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:09:35 UTC


README

Packagist License MIT HHVM Tested PHP 7 Supported Build Status Code Climate Test Coverage

创建易于可视化和查看内容的HTML表格。

安装

将以下内容添加到您的 composer.json 文件中。

{
    "require": {
        "bootpress/table": "^1.0"
    }
}

简单示例

<?php

use BootPress\Table\Component as Table;

$table = new Table;

$html = $table->open();
    $html .= $table->row();
    $html .= $table->cell('', 'One');
    $html .= $table->cell('', 'Two');
    $html .= $table->cell('', 'Three');
$html .= $table->close();

echo $html;

这将给你一行三个单元格

换句话说

<table>
    <tbody>
        <tr>
            <td>One</td>
            <td>Two</td>
            <td>Three</td>
        </tr>
    </tbody>
</table>

跨列和跨行

请注意,我们使用的属性语法既紧凑又易于阅读。基本上,每个属性都由一个 '|'(单竖线)分隔,我们省略了引号。

$html = $table->open('border=1|class=special');
    $html .= $table->row();
    $html .= $table->cell('rowspan=2', 'Two Rows');
    $html .= $table->cell('', 'One');
    $html .= $table->cell('', 'Two');
    $html .= $table->row();
    $html .= $table->cell('colspan=2', 'Buckle my shoe');
$html .= $table->close();

echo $html;
<table border="1" class="special">
    <tbody>
        <tr>
            <td rowspan="2">Two Rows</td>
            <td>One</td>
            <td>Two</td>
        </tr><tr>
            <td colspan="2">Buckle my shoe</td>
        </tr>
    </tbody>
</table>

标题,表头和页脚

不需要向方法传递单元格内容。它仍然会被适当地包装。

$html = $table->open('border=1', 'Caption');
    $html .= $table->head();
    $html .= $table->cell('colspan=2') . 'Header';
    $html .= $table->row();
    $html .= $table->cell() . 'Three';
    $html .= $table->cell() . 'Four';
    $html .= $table->foot();
    $html .= $table->cell('colspan=2') . 'Shut the door';
$html .= $table->close();

echo $html;
<table border="1">
    <caption>Caption</caption>
    <thead>
        <tr>
            <th colspan="2">Header</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Three</td>
            <td>Four</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="2"> Shut the door</td>
        </tr>
    </tfoot>
</table>

嵌套表格

当你 $table->close() 时,所有内容都会重置,你可以无任何问题地进行另一个 $table->open()。但对于嵌套表格,你需要创建类的另一个实例。

$t1 = new Table;
$t2 = new Table;

$html = $t1->open();
    $html .= $t1->row();
    $html .= $t1->cell('', 'Five');
    $html .= $t1->cell() . 'Six';
    $html .= $t1->cell();
        $html .= $t2->open('border=1');
            $html .= $t2->row();
            $html .= $t2->cell('', 'Pick');
            $html .= $t2->cell('', 'Up');
            $html .= $t2->cell('', 'Sticks');
        $html .= $t2->close();
$html .= $t1->close();
<table>
    <tbody>
        <tr>
            <td>Five</td>
            <td>Six</td>
            <td>
                <table border="1">
                    <tbody>
                        <tr>
                            <td>Pick</td>
                            <td>Up</td>
                            <td>Sticks</td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。