PHP code example of jpustkuchen / tableclass-bundle
1. Go to this page and download the library: Download jpustkuchen/tableclass-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jpustkuchen / tableclass-bundle example snippets
use JPustkuchen\TableClassBundle\Elements\TableRow;
use JPustkuchen\TableClassBundle\TableFactory;
class TsfAgDefaultController extends AbstractController
{
public function indexAction(Request $request, TableFactory $tableFactory): Response
{
$results = [/* Your database query results or custom array as keyed array with row keys as value keys */
['col1' => 'Row 1 Cell 1 Value', 'col2' => 'Row 1 Cell 2 Value'],
['col1' => 'Row 2 Cell 1 Value', 'col2' => 'Row 2 Cell 2 Value'],
// ...
];
// TableFactory via dependency injection
$tableMain = $tableFactory->createTable()
// Set DataTable classes (optional - just as example)
->addClassesFromArray(['ui', 'selectable', 'celled', 'striped', 'stackable', 'table'])
->setHeaderFromArray([
'col1' => 'Col 1 Header Label',
'col2' => 'Col 2 Header Label',
'tableActions' => 'Actions Example Row',
])->addRowsFromArray($results);
// Alternatively you could also add rows and cols manually by ->addRow() or ->addColumn().
// Iterate rows for actions on the data:
$tableMain->iterateRows(function (TableRow $row, $index) {
// Add classes based on cell values:
$cellRow1 = $row->getCellbyKey('row1');
$cellRow1Value = $cellRow1->getValue();
if ($y) {
// Add class 'warning' to row if condition is met
$row->addClass('warning');
}
if ($z) {
// Add class 'error' to cell if condition is met
$cellRow1->addClass('error');
}
return $row;
});
// The contained table.html.twig expects the table array structure in the key 'tabledata':
return $this->render('table.html.twig', [
'tabledata' => $tableMain->toArray(),
]);
}
}