Skip to content

Commit

Permalink
Added removeHeaderlessColumns helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
JPustkuchen committed Jun 11, 2021
1 parent 88b707a commit 80cd35d
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/Elements/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public function setRows(array $rows = []): Table {
*
* @param [type] $lambda
*/
public function iterateRows(callable $lambda){
foreach($this->rows as $index => $row){
public function iterateRows(callable $lambda) {
foreach ($this->rows as $index => $row) {
$this->rows[$index] = $lambda($row, $index);
}
}
Expand All @@ -140,9 +140,36 @@ public function toArray(): array {
/**
* Returns the rendered
*/
public function render(){
public function render() {
return $this->twig->render('@JPustkuchenTableClass/table.html.twig', [
'tabledata' => $this->toArray()
]);
}

/**
* Helper function to remove headerless columns from all rows.
* You may for example find this useful when using
* setHeaderFromArray() + addRowsFromArray() with different array sizes.
*
* Only useful if header was set before.
*
* @return array List of all removed headerless column keys.
*/
public function removeHeaderlessColumns() {
$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 $removedColumns;
}
}

0 comments on commit 80cd35d

Please sign in to comment.