lasyntez/table-generator

面向对象的PHP类,用于生成表格

dev-master 2017-12-31 18:20 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:14:48 UTC


README

面向对象的PHP库,用于生成HTML表格

需求

  • PHP 5.5+

安装

 $ composer require lasyntez/table-generator

基本用法

前端开发者应该知道,创建表格有时可能很棘手。这个库提供了一个优雅且直观的方式,帮助他们静态或动态地轻松生成表格。

<?php

    require_once('vendor/autoload.php');

	use TableGenerator\HTMLTableGenerator\Structure\Table;
	use TableGenerator\HTMLTableGenerator\Structure\Row;
	use TableGenerator\HTMLTableGenerator\Structure\Cell;
	use TableGenerator\HTMLTableGenerator\Structure\TDCell;
	use TableGenerator\HTMLTableGenerator\Structure\THCell;
    use TableGenerator\HTMLTableGenerator\Attributes\AttributesHandler;
	use TableGenerator\Storage\SplStorage;
	use TableGenerator\Storage\ArrayStorage;

    /** Instantiate the table */
	$table = new Table(new ArrayStorage, new AttributesHandler(array('id' => 'planets')));

    /** Create the rows with the cells */
	$tr = new Row(new SplStorage);

    /** Create a th cell with a colspan value of 3 and add it to the first row */
    $th = new THCell('Planets', 3);
	$tr->addCell($th);

	$tr2 = new Row(new ArrayStorage);
	$tr2->addCell(new TDCell('Jupiter', 1, 1, Cell::WIDTH_UNDEFINED, Cell::HEIGHT_UNDEFINED))
      ->addCell(new TDCell('Mars', Cell::COLSPAN_REGULAR, Cell::ROWSPAN_REGULAR))
      ->addCell(new TDCell('Pluton', 1, 1))
  ;

	$tr3 = new Row(new SplStorage, new AttributesHandler(array('id' => 'orion')));
	$tr3->addCells(array(
		new TDCell('Neptune', 1, 1, Cell::WIDTH_UNDEFINED, Cell::HEIGHT_UNDEFINED),
		new TDCell('Saturn', Cell::COLSPAN_REGULAR, Cell::ROWSPAN_REGULAR),
		new TDCell('Venus', 1, 1),
	));

	/** Add the rows to the table individually  */
	$table->addRow($tr)
		    ->addRow($tr2)
	      ->addRow($tr3)
  ;

	/** Or collectively  */
	$table->addRows(array($tr2, $tr, $tr3));

    /** Generate the html code */
	$html = $table->generate();

    /** And display it on the web page */
	echo $html;

子元素存储

表元素(例如,表和行)应该包含其他元素,必须设置存储类型(ArrayStorage或SplStorage),它们实现了StorageInterface,以便处理子元素。具体来说,它允许向表格中添加行或向行中添加单元格,以及它们的删除。

<?php

$table = new Table(new ArrayStorage);

/** OR */

$table = new Table(new SplStorage);

HTML属性

表元素的HTML属性由AttributesHandler处理。其作用是为表格、行或单元格添加HTML属性,从而允许CSS和JS交互。支持的属性有:id、class和style。

<?php

/** To add an id to a table */

/** VIA THE CONSTRUCTOR */
/** Create an instance of AttributesHandler by providing an array containing the attributes with their values  */
$handler = new AttributesHandler(array('id' => 'planets'));
/** Then pass it as the second parameter in The Table's Constructor */
$table = new Table(new SplStorage, $handler);

/** OR VIA THE setAttributesHandler METHOD */
$table = new Table(new ArrayStorage);
$table->setAttributesHandler($handler);

单元格创建

要创建单元格,无论是THCell还是TDCell,内容是第一个也是唯一的必需参数。其他参数(colspan、rowspan、width、height和AttributesHandler)是可选的。

<?php

$td = new TDCell('<a href="planets/pluton.html">Pluton</a>');

/** TO SET A WIDTH OF 300px AND A HEIGHT OF 200px */
$td = new THCell('Pluton', Cell::COLSPAN_REGULAR, Cell::ROWSPAN_REGULAR, 300, 200);

/** TO SET A COLSPAN OF 3 AND A ROWSPAN OF 5 */
$td = new THCell('Pluton', 3, 5, 300, 200);

/** ADD A CLASS ATTRIBUTE BY PASSING AN INSTANCE OF AttributesHandler AS THE LAST PARAMETER IN THE CELL'S CONSTRUCTOR */
$td = new THCell('Pluton', Cell::COLSPAN_REGULAR, Cell::ROWSPAN_REGULAR, Cell::WIDTH_UNDEFINED, Cell::HEIGHT_UNDEFINED, new AttributesHandler(array('class' => 'pluton')));

许可证

MIT许可证。查看文件