This section provides examples of how to utilize Table Generator's components to construct tables dynamically.
Creating a Basic Table
You can create a simple table using Table
and its child components:
use Ucscode\HtmlComponent\TableGenerator\Table;
use Ucscode\HtmlComponent\TableGenerator\Component\Thead;
use Ucscode\HtmlComponent\TableGenerator\Component\Tbody;
use Ucscode\HtmlComponent\TableGenerator\Component\Section\Tr;
use Ucscode\HtmlComponent\TableGenerator\Component\Section\Th;
use Ucscode\HtmlComponent\TableGenerator\Component\Section\Td;
$thead = new Thead([
new Tr([
new Th("Id"),
new Th("Name"),
new Th("Email")
])
]);
$tbody = new Tbody([
new Tr([
new Td("1"),
new Td("John Doe"),
new Td("john@example.com")
]),
new Tr([
new Td("2"),
new Td("Jane Doe"),
new Td("jane@example.com")
])
]);
$table = new Table();
$table
->setThead($thead)
->addTbody($tbody)
;
echo $table->render();
The Output
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John Doe</td>
<td>john@example.com</td>
</tr>
<tr>
<td>2</td>
<td>Jane Doe</td>
<td>jane@example.com</td>
</tr>
</tbody>
</table>
Adding Rows Dynamically
You can dynamically add rows to a Tbody
after creation:
$tbody = new Tbody();
$tbody->addTr(new Tr([
new Td("3"),
new Td("Alice"),
new Td("alice@example.com")
]));
Using Collections to Manage Rows
Instead of consecutively adding Tr
elements, you can use TrCollection
:
use Ucscode\HtmlComponent\TableGenerator\Collection\TrCollection;
$rows = new TrCollection([
new Tr([
new Td("4"),
new Td("Bob"),
new Td("bob@example.com")
]),
new Tr([
new Td("5"),
new Td("Charlie"),
new Td("charlie@example.com")
])
]);
$tbody->setTrCollection($rows);
The method Tbody::setTrCollection()
will actually replace the existing TrCollection
.
Instead of replacing, you can append more rows by getting the TrCollection
instance that exists in the Tbody
Example:
$trCollection = $tbody->getTrCollection();
$trCollection->append(new Tr());
$trCollection->append(new Tr());
Customizing Table Attributes
You can set attributes such as class
, style
, data-*
etc for table elements in 2 ways:
1. Passing it to the constructor.
Depending on the table component being instantiated, the attributes may be accepted in the first or second argument
// The first parameter of Tbody accepts null, a TrCollection or an array of Tr
$tbody = new Tbody(null, [
'class' => 'tbody',
'data-id' => 'uuid-0001',
]);
// The parameter of Table accepts an Attributes instance or an array of attributes
$table = new Table(['class' => 'table']);
2. Getting the Attributes
instance and updating it
$table->getAttributes()
->set("class", "table table-striped")
->set("id", "user-table")
;
When rendered, this generates:
<table class="table table-striped" id="user-table">...</table>
Tips for passing attributes
Component | Parameter 1 | Parameter 2 |
---|---|---|
Table & Col |
Attributes | — |
Caption & CellInterface |
Arbitrary Data | Attributes |
Others | Array or Collection | Attributes |
These examples showcase the flexibility and ease of use of Table Generator, allowing developers to build structured and dynamic tables efficiently.