tlr/html-table-builder 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tlr/html-table-builder

Composer 安装命令:

composer require tlr/html-table-builder

包简介

An HTML Table Builder

README 文档

README

Build Status

A class-based, "fluent" HTML table builder in PHP.

use Tlr\Tables\Elements\Table;

$table = new Table;

$table->class('ui celled table');

$row = $table->header()->row();
$row->cell('Name');
$row->cell('Age');
$row->cell('Something Else');

$row = $table->body()->row();

$row->cell('George');
$row->cell('12');
$row->cell('Food');

$table->footer()->row()
    ->cell('<a href="/create">Add a new entry</a>')->dontEscape()->span(3);

echo $table->render();

Installation

composer require tlr/html-table-builder

Basic Usage

You can see much of the functionality in the above example. More detailed explanations are below.

Reference

Table

The table object represents a top level HTML table.

See Classable below for information on setting the rows' classes.

See Attributable below for information on setting other attributes on the cell element.

It has four other main methods.

$table->render()

This is a helpful shortcut to doing the following:

(new TableRenderer)->renderElement($table);

If you want to pretty print the HTML in your table, you can set it like so:

(new TableRenderer)->prettyPrint()->renderElement($table);

$table->header() $table->body() $table->footer()

These all return a table section object (see below).

These will be initialised the first time you call the function, so if you never call ->header(), then a <thead> section will not be rendered.

You can call them multiple times, and they will return the same object.

Section (thead, tbody, tfoot)

These objects represent sections in a table.

See Classable below for information on setting the rows' classes.

See Attributable below for information on setting other attributes on the cell element.

There are two other methods to talk about here.

$section->row()

This initialises and returns a new Row object. Calling it multiple times will add multiple rows to the table.

$section->nextRow(Row $current)

Returns the next row in its children from the one passed to it.

Throws an error if passed a row that is not one of its children.

See the code example for the logic of how this works.

$one = $section->row();
$two = $section->row();

$section->nextRow($one) === $two; // true
$section->nextRow($two); // a new row

Row

These objects represent rows in a table section.

See Classable below for information on setting the rows' classes.

See Attributable below for information on setting other attributes on the cell element.

$row->addCell(CellInterface $cell)

This adds a new cell to the row. You can use this to add a custom cell to the row.

The following helper methods use this method to add the basic included cell types.

  • $row->cell(string $content = null)
  • $row->linkCell(string $link)
  • $row->imageCell(string $src)

Cells

These objects represent cells in a table row.

See Spannable below for information on setting the column spanning.

See Classable below for information on setting the cells' classes.

See Attributable below for information on setting other attributes on the cell element.

Content Cell

This is the default cell. It is fairly versatile, although basic.

You can set the content on initialiastaion, or by calling the ->content(string $content) method.

The default behaviour is to escape the content. If you want to override this, chain in the ->dontEscape() or ->raw() methods.

There is a ->wrapContent(string $open, string $content, string $close) method. This lets you enter some unescaped HTML that wrap some escaped content. Essentially, all this does is escape the $content value, then concatenate them in order, and set the cell to ->raw() or ->dontEscape(). Nevertheless, it can still be helpful - allowing you to mix your own unescaped HTML with some content that should be escaped.

As with any unescaped content, it is up to you to ensure the HTML is valid.

See below for some examples.

$row->cell('Cat'); // <td>Cat</td>
$row->cell('<Cat>'); // <td>&lt;Cat&gt;</td>
$row->cell('<Cat>')->raw(); // <td><Cat></td>
$row->cell()->content('Cat'); // <td>Cat</td>
$row->cell()->wrapContent('<strong>', '<Cat>', '</strong>'); // <strong>&lt;Cat&gt;</strong>

Link Cell

A link cell is a cell with a link in it. Other than the link argument to its constructor, it behaves like a ContentCell

$row->linkCell('https://google.com')->content('Visit Google.');
// <td><a href="https://google.com">Visit Google.</a></td>

$cell = new LinkCell('localhost')->content('<pre>some preformatted text</pre>')->raw();

Image Cell

$row->imageCell('cat.jpg');
// <td><img src="cat.jpg" /></td>

Traits

Spannable

On the Cell objects, you can call:

// setting rowspan
$row->cell('A double height column')->spanRows(2);

// setting colspan
$row->cell('A triple width column')->spanColumns(3);

This returns the element object for chaining.

Classable

On any of the element objects - Table, Section, Row, Cell, you can set the class. The following three examples all result in <table class="ui table">:

// This is the semantic ui CSS framework table class
$table->class('ui table');

// You don't have to set the classes at the same time.
$table->class('ui')->class('table');

// You can pass an array of string
$table->class(['ui', 'table']);

This returns the element object for chaining.

Attributable

You can set any other HTML attribute:

$table->attribute('foo', 'bar');

This returns the element object for chaining.

tlr/html-table-builder 适用场景与选型建议

tlr/html-table-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.31k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2016 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tlr/html-table-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tlr/html-table-builder 我们能提供哪些服务?
定制开发 / 二次开发

基于 tlr/html-table-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 40.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-25