andydune / html-table
此代码将简化或改进您使用HTML表格的工作。
v1.3.1
2019-04-26 12:29 UTC
Requires
- php: >=5.6
- andydune/string-replace: 1.*
Requires (Dev)
- phpunit/phpunit: ^5.7.15 || ^6.0.8
README
此代码将简化或改进您使用HTML表格的工作。
需求
PHP版本 >= 5.6
安装
使用composer安装
composer require andydune/html-table
或者如果composer未全局安装
php composer.phar require andydune/html-table
或者编辑你的 composer.json
"require" : {
"andydune/html-table": "^1"
}
执行以下命令
php composer.phar update
示例
以下是绘制带有动态数据的HTML表格的代码片段。
<table class="table authlog">
<thead>
<tr>
<td>ID</td>
<td>User</td>
<td>STATUS</td>
<td>Created</td>
<td>Updated</td>
<td>Uploaded</td>
<td>Link</td>
<td>Data</td>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<?php foreach ($list as $file) {
?>
<tr class="<?= $file->getStatus() == 2 ? 'table-success' : null; ?>">
<td><?= $file->getId(); ?></td>
<td><?= $user['EMAIL'] ?> (<?= $user['XML_ID'] ?>)</td>
<td><?= $showStatus($file->getStatus()); ?></td>
<td><?= $file->getData('DATETIME'); ?></td>
<td><?= $file->getData('DATETIME_UPDATE'); ?></td>
<td><?= $file->getData('DATETIME_LAST_REQUEST'); ?></td>
<td><?= $file->getFileName(); ?>.<?= $file->getFileType() ?></td>
<td><? $showArray($file->getMeta()) ?></td>
<td></td>
<td>повтор</td>
<td>удаление</td>
</tr>
<?php } ?>
</tbody>
</table>
不小心删除单个标签就可能导致HTML结构破坏,而且难以阅读和修改。
下面有更好的代码
use AndyDune\HtmlTable\Builder; use AndyDune\HtmlTable\Table; $table = new Table(); $head = $table->head(); $head->cell()->setContent('ID'); $head->cell()->setContent('User'); $head->cell()->setContent('STATUS'); $head->cell()->setContent('Created'); $head->cell()->setContent('Updated'); $head->cell()->setContent('Uploaded'); $head->cell()->setContent('Uploaded'); $head->cell()->setContent('Data'); // Empty cells will be added automatically depends on max cell count for next rows. foreach ($list as $file) { $row = $table->row(); if ($file->getStatus() == 2) { $row->addClass('table-success') } $row->cell()->setContent($file->getId()); $row->cell()->setContent($user['EMAIL'] . $user['XML_ID']); $row->cell()->setContent($showStatus($file->getStatus())); $row->cell()->setContent($file->getData('DATETIME')); $row->cell()->setContent($file->getData('DATETIME_UPDATE')); $row->cell()->setContent($file->getData('DATETIME_LAST_REQUEST')); $row->cell()->setContent($file->getFileName() . '.' . $file->getFileType()); $row->cell()->setContent($showArray($file->getMeta())); } $buider = new Builder($table); $builder->setGroupingSections(true); echo $buider->getHtml();
类结构
表格结构通过类来反映
-
AndyDune\HtmlTable\Table- 结构的根 -
AndyDune\HtmlTable\Element\Row- 实现表格行 -
AndyDune\HtmlTable\Element\Head- 实现特殊行(表头)。只有一个。 -
AndyDune\HtmlTable\Element\Cell- 实现表格单元格。它是Row(Head) 的一部分 -
AndyDune\HtmlTable\Builder- 构建表格HTML代码的根类。它以Table实例作为构造参数。 -
有许多辅助类用于构建表格,但你不需要了解它们。
描述表格
表格可能有属性、数据行、表头行。行和单元格也可能有属性。
表格、行、单元格类属性
使用 addClass 方法向表格元素注入类。元素可以有多个类
use AndyDune\HtmlTable\Table; // Set classes *useful* and *one* to table. $table = new Table(); $table->addClass('useful')->addClass('one'); $table->getClasses(); ['useful', 'one] // <table class="useful one"> // Set class *active* to row. $row = $table->row(); $row->addClass('active'); // <tr class="active"> // Set class *left* to cell. $cell = $row->cell(); $row->addClass('left'); // <td class="left">
表格、行、单元格id属性
使用 setId 方法向表格元素注入id。元素只有一个id
use AndyDune\HtmlTable\Table; // <table id="top"> $table = new Table(); $table->setId('top'); $table->getId(); // top // <tr id="active"> $row = $table->row(); $row->setId('active'); $row->getId(); // active // <td id="left"> $cell = $row->cell(); $row->setId('left'); $row->getId(); //left