, (*1)
Cellbrush table generator
A library to generate HTML tables with PHP., (*2)
Table structure:, (*3)
- Named rows and columns, so they can be targeted with string keys.
- Colspan and rowspan using col groups and row groups.
- Automatically fills up empty cells, to preserve the structural integrity.
- Automatically warns on cell collisions.
Tag attributes:, (*4)
- Easily add row classes.
- Easily add row striping classes (odd/even zebra striping and more).
- Easily add column classes that apply to all cells in the column.
- (more planned)
API design:, (*5)
- Method chaining instead of huge arrays of doom.
- Shortcut notations for frequently used stuff.
- Return value and parameter types nicely documented, so your IDE can let you know about possible operations.
- Exceptions thrown for integrity violation.
- Composer and PSR-4.
Basic usage
A simple 3x3 table with the diagonal cells filled., (*6)
$table = \Donquixote\Cellbrush\Table\Table::create()
->addRowNames(['row0', 'row1', 'row2'])
->addColNames(['col0', 'col1', 'col2'])
->td('row0', 'col0', 'Diag 0')
->td('row1', 'col1', 'Diag 1')
->td('row2', 'col2', 'Diag 2')
;
$html = $table->render();
A table like above, but with added thead section., (*7)
Column names are shared between table sections, but new rows need to be defined for each section., (*8)
$table = ...
$table->thead()
->addRowName('head row')
->th('head row', 'col0', 'H0')
->th('head row', 'col1', 'H1')
->th('head row', 'col2', 'H2')
;
$html = $table->render();
H0 |
H1 |
H2 |
Diag 0 |
|
|
|
Diag 1 |
|
|
|
Diag 2 |
Additional tbody sections
By default, every addRowName() and td() or th() goes into the main tbody section., (*9)
So, the following two are equivalent:, (*10)
$table->td('row0', 'col0', 'Cell contents');
$table->tbody()->td('row0', 'col0', 'Cell contents');
More named tbody sections can be added like this:, (*11)
$table->tbody('tb1')
->addRowName(..)
->td(..)
Again, the column definitions are shared between table sections, but row
definitions need to be added separately., (*12)
Full rowspan and colspan
To let a cell span the entire width of the table, simply set the column name to ''.
Likewise, set the row name to '' to span the entire height of the table section., (*13)
$table->thead()
->addRowName('head row')
->td('head row', '', 'Horizontal cell in thead.')
;
$table
->...
->td('', 'col1', 'Vertical cell')
;
Horizontal cell in thead. |
# |
Vertical cell |
# |
# |
# |
# |
# |
Column groups
Named column groups allow for cells with colspan.
In the below example, the column name "products" specifies a colspan cell that spans all 3 products.* cells, whereas "products.a", "products.b" and "products.c" specifies specific cells without colspan., (*14)
$table
->addColName('legend')
->addColGroup('products', ['a', 'b', 'c'])
;
$table->thead()
->addRowName('head')
->th('head', 'legend', 'Legend')
// The "Products" label will span 3 columns: products.a, products.b, products.c
->th('head', 'products', 'Products')
->addRowName('name')
->th('name', 'legend', 'Product name')
->th('name', 'products.a', 'Product A')
->th('name', 'products.b', 'Product B')
->th('name', 'products.c', 'Product C')
;
$table
->addRowName('width')
->th('width', 'legend', 'Width')
->td('width', 'products.a', '55 cm')
->td('width', 'products.b', '102 cm')
..
->addRowName('height')
..
->addRowName('price')
->td('price', 'products.a', '7.66 EUR')
Legend |
Products |
Product name |
Product A |
Product B |
Product C |
Width |
55 cm |
102 cm |
7 cm |
Row groups
Similar to column groups., (*15)
$table = Table::create()
->addColNames(['legend', 'sublegend', 0, 1])
->addRowGroup('dimensions', ['width', 'height'])
->addRowName('price')
->th('dimensions', 'legend', 'Dimensions')
->th('dimensions.width', 'sublegend', 'Width')
->th('dimensions.height', 'sublegend', 'Height')
->th('price', 'legend', 'Price')
;
$table->headRow()->thMultiple(['Product 0', 'Product 1']);
$table->rowHandle('dimensions.width')->tdMultiple(['2cm', '5cm']);
$table->rowHandle('dimensions.height')->tdMultiple(['14g', '22g']);
$table->rowHandle('price')->tdMultiple(['7,- EUR', '5,22 EUR']);
|
|
Product 0 |
Product 1 |
Dimensions |
Width |
2cm |
5cm |
Height |
14g |
22g |
Price |
|
7,- EUR |
5,22 EUR |
Combination of row groups and column groups
$table = (new Table())
->addColName('name')
->addColGroup('info', ['color', 'price'])
->addRowGroup('banana', ['description', 'info'])
->th('banana', 'name', 'Banana')
->td('banana.description', 'info', 'A yellow fruit.')
->td('banana.info', 'info.color', 'yellow')
->td('banana.info', 'info.price', '60 cent')
->addRowGroup('coconut', ['description', 'info'])
->th('coconut', 'name', 'Coconut')
->td('coconut.description', 'info', 'Has liquid inside.')
->td('coconut.info', 'info.color', 'brown')
->td('coconut.info', 'info.price', '3 dollar')
;
$table->headRow()
->th('name', 'Name')
->th('info.color', 'Color')
->th('info.price', 'Price')
;
Name |
Color |
Price |
Banana |
A yellow fruit. |
yellow |
60 cent |
Coconut |
Has liquid inside. |
brown |
3 dollar |
Shortcut syntax with row handles and column handles
RowHandle and *ColHandle allow you to omit one of $rowName and $colName to address a table cell., (*16)
$table = (new Table())
->addRowNames(['row0', 'row1', 'row2'])
->addColNames(['legend', 'col0', 'col1', 'col2'])
...
;
// Add cells in a "head0" row in the thead section.
$table->headRow()
->th('col0', 'Column 0')
->th('col1', 'Column 1')
->th('col2', 'Column 2')
;
// Add cells in a "legend" column.
$table->colHandle('legend')
->th('row0', 'Row 0')
->th('row1', 'Row 1')
->th('row2', 'Row 2')
;
|
Column 0 |
Column 1 |
Column 2 |
Row 0 |
Diag 0 |
|
|
Row 1 |
|
Diag 1 |
|
Row 2 |
|
|
Diag 2 |
Row classes
Row classes can be added quite easily with addRowClass()
., (*17)
$table->addRowClass('row0', 'rowClass0');
Row striping
Row striping classes can be added to a table section with addRowStriping()
., (*18)
The default striping is ['odd', 'even']
, but different patterns can be added with three or more stripes., (*19)
// Odd/even zebra striping.
$table->addRowStriping();
// 3-way striping.
$table->addRowStriping(['1of3', '2of3', '3of3']);
The striping always applies to a table section. By default, this wil be the main tbody section., (*20)
Column classes
You can use addColClass()
to add a class to all cells of a column.
This can be done either for all table sections at once, or for specific table sections., (*21)
$table->addColClass('col0', 'allSectionsColumn0');
$table->tbody()->addColClass('col0', 'tbodyColumn0');
More examples?
You can see more examples in the unit tests., (*22)
Planned features
Next steps:, (*23)
- Nested groups, more than one level deep.
- Brick-style ("overlapping") rowspan and colspan, e.g. to represent time intervals.
- More options to set html tag attributes and classes on td/th, tr, tbdody/thead/tfoot, and the table itself.
Ideas and pull requests are welcome!
- Open-end cells. You only mark the top left, the rest will expand until it hits something.
- Reordering of rows and columns after the cells are filled.
- Insertion of new rows and columns after the cells are filled.