Skip to content

Commit

Permalink
Added hideHeaderlessColumns
Browse files Browse the repository at this point in the history
  • Loading branch information
JPustkuchen committed Jun 11, 2021
1 parent 5f3571c commit 4148793
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/Elements/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,41 @@ public function render() {
*/
public function removeHeaderlessColumns(): Table {
$cellKeys = $this->header->getCellKeys();
$removedColumns = [];
if (!empty($this->rows) && !empty($cellKeys)) {
foreach ($this->rows as $row) {
$cells = $row->getCells();
foreach ($cells as $cell) {
$cellKey = $cell->getKey();
if (!in_array($cellKey, $cellKeys)) {
$row->removeCell($cellKey);
$removedColumns[$cellKey] = $cellKey;
}
}
}
}
return $this;
}

/**
* Helper function to hide headerless columns from all rows in twig output.
* You may for example find this useful when using
* setHeaderFromArray() + addRowsFromArray() with different array sizes.
*
* Only useful if header was set before.
*
* This
* @param stromg $class The class name to hide the cell.
* @return Table
*/
public function hideHeaderlessColumns($class = 'hidden'): Table {
$cellKeys = $this->header->getCellKeys();
$removedColumns = [];
if (!empty($this->rows) && !empty($cellKeys)) {
foreach ($this->rows as $row) {
$cells = $row->getCells();
foreach ($cells as $cell) {
$cellKey = $cell->getKey();
if (!in_array($cellKey, $cellKeys)) {
$cell->addClass($class);
}
}
}
Expand Down

0 comments on commit 4148793

Please sign in to comment.