In Table Generator, a Cell
represents an individual data block within a table. Cells can either be a table header (Th
) or a table data (Td
) element.
Cell Implementation
abstract class AbstractCell implements CellInterface, ArbitraryDataInterface {...}
Available Cell Components
Both Th
and Td
extend the AbstractCell
.
Cell Component | Description |
---|---|
Th |
Represents a table header cell, typically used within a Thead . |
Td |
Represents a table data cell, used within Tbody or Tfoot . |
The CellInterface
is a marker that defines the standard behavior of table cells.
interface CellInterface {}
The ArbitraryDataInterface
provides methods for handling arbitrary data within a cell.
interface ArbitraryDataInterface {
public function getData(): mixed;
public function setData(mixed $data): static;
}
The ArbitraryDataInterface
methods can accept and return literally anything. It if knows how to render it on the generated html table, it will. Otherwise, it will ignore it, leaving an empty cell.
Creating a Cell
You can create a Th
or Td
component and assign it content:
new Th("Product Name");
new Td("Laptop");
Setting and Retrieving Data
Since Th
and Td
implement ArbitraryDataInterface
, they support dynamic data manipulation:
$cell->setData("Smartphone");
echo $cell->getData(); // Outputs: Smartphone
Working with elements
Cells can accept and handle elements of type NodeInterface
. Here's a simple example:
$buttonNode = new ElementNode(NodeNameEnum::NODE_BUTTON);
$buttonNode->setAttribute('class', 'btn btn-primary');
$td = new Td($buttonNode);
// or
$td->setData($buttonNode);
For more information on working with Nodes, see UssElement
It is also possible to present a table component as a cell data and the table generator will be able to handle it.
This can be useful when creating an email compactible message which requires tables upon tables.
$table = (new Table())
->setThead(new Thead())
->addTbody(new Tbody())
;
$td->setData($table);
Cells are fundamental building blocks of any table structure, enabling both static and dynamic content insertion.