devyanlab / surface
PHP 中灵活的 HTML 表格生成
dev-master
2014-10-03 14:01 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-24 02:16:42 UTC
README
Surface 允许通过流畅且易于使用的接口生成语义正确的 HTML 表格。
它被设计得尽可能灵活,并支持属性和表格嵌套。
##要求
- PHP 5.3,因为使用了命名空间和闭包
##使用 ### 创建简单表格 创建表格很简单
// Some data
$rows = array(
array('China', '1,354,040,000', '19.07'),
array('India', '1,210,569,573', '17.05'),
array('United States', '316,278,000', '4.46'),
array('Indonesia', '237,641,326', '3.35'),
array('Brazil', '193,946,886', '2.73')
);
// Create
$surface = new Surface\Surface();
// Configure
$surface->setHead(array('Country', 'Population', '% of world'))
->addRows($rows);
// Render
echo $surface->render();
上述代码将输出以下 HTML
<table>
<thead>
<tr>
<th>Country</th>
<th>Population</th>
<th>% of world</th>
</tr>
</thead>
<tbody>
<tr>
<td>China</td>
<td>1,354,040,000</td>
<td>19.07</td>
</tr>
<tr>
<td>India</td>
<td>1,210,569,573</td>
<td>17.05</td>
</tr>
<tr>
<td>United States</td>
<td>316,278,000</td>
<td>4.46</td>
</tr>
<tr>
<td>Indonesia</td>
<td>237,641,326</td>
<td>3.35</td>
</tr>
<tr>
<td>Brazil</td>
<td>193,946,886</td>
<td>2.73</td>
</tr>
</tbody>
</table>
###表格页脚
要向表格添加页脚,只需使用 setFoot()
方法
$surface->setHead(array('Country', 'Population', '% of world'))
->addRows($rows)
->setFoot(array('Total', 'Some total figure here'));
echo $surface->render();
这将生成使用 <tfoot>
元素的表格 HTML
<table>
<thead>
<tr>
<th>Country</th>
<th>Population</th>
<th>% of world</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Total</td>
<td>Some total figure here</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>China</td>
<td>1,354,040,000</td>
<td>19.07</td>
</tr>
<tr>
<td>India</td>
<td>1,210,569,573</td>
<td>17.05</td>
</tr>
<tr>
<td>United States</td>
<td>316,278,000</td>
<td>4.46</td>
</tr>
<tr>
<td>Indonesia</td>
<td>237,641,326</td>
<td>3.35</td>
</tr>
<tr>
<td>Brazil</td>
<td>193,946,886</td>
<td>2.73</td>
</tr>
</tbody>
</table>
###使用属性
可以在元素级别管理 Surface 的部分,这意味着可以为每个元素提供自己的属性
// Create a table as usual, we can pass some attributes too
$surface = new Surface\Surface(array('id' => 'population-data'));
$surface->setHead(array('Country', 'Population', '% of world'))
->addRows($rows);
// Some new data to add to existing rows
$newData = array(
'Pakistan',
// We can specify and manipulate the data at this level
new Surface\Data('183,711,000', array('class' => 'td-green-highlight')),
'2.59'
);
// Construct a new row and pass it to the table
$row = new Surface\Row($newData, array('class' => 'custom-row'));
$surface->addRow($row);
// Render
echo $surface->render();
###嵌套表格
使用 Surface 嵌套表格也很容易实现。只需将现有表格作为数据项传递给新的表格
// $nestedTable created up here
$parentTable = new Surface\Surface();
$parentTable->setHead(array('Country', 'Population', '% of world'))
->addRows($rows)
->addRow(array($nestedTable)); // $nestedTable is an instance of Surface\Surface
// Easy!
echo $parentTable->render();
这将生成以下输出
<table>
<thead>
<tr>
<th>Country</th>
<th>Population</th>
<th>% of world</th>
</tr>
</thead>
<tbody>
<tr>
<td>China</td>
<td>1,354,040,000</td>
<td>19.07</td>
</tr>
<tr>
<td>India</td>
<td>1,210,569,573</td>
<td>17.05</td>
</tr>
<tr>
<td>United States</td>
<td>316,278,000</td>
<td>4.46</td>
</tr>
<tr>
<td>Indonesia</td>
<td>237,641,326</td>
<td>3.35</td>
</tr>
<tr>
<td>Brazil</td>
<td>193,946,886</td>
<td>2.73</td>
</tr>
<tr>
<td>
<table>
<thead>
<th>Id</th>
<th>Name</th>
<th>Date Created</th>
</thead>
<tbody>
<tr>
<td>845</td>
<td>User 845</td>
<td>2013-01-01</td>
</tr>
<tr>
<td>384</td>
<td>User 384</td>
<td>2012-05-10</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>