Skip to content

Commit 6092763

Browse files
dimtrovichadhocore
authored andcommitted
docs: update readme to add doc for table customization
1 parent f74236b commit 6092763

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

README.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,12 +609,70 @@ $writer->table([
609609
'head' => 'boldGreen', // For the table heading
610610
'odd' => 'bold', // For the odd rows (1st row is odd, then 3, 5 etc)
611611
'even' => 'comment', // For the even rows (2nd row is even, then 4, 6 etc)
612+
'1:1' => 'red', // For cell in row 1 col 1 (1 based count, 'apple' in this example)
613+
'2:*' => '', // For all cells in row 2 (1 based count)
614+
'*:2' => '', // For all cells in col 2 (1 based count)
615+
'b-c' => '', // For all columns named 'b-c' (same as '*:2' in this example)
616+
'*:*' => 'blue', // For all cells in table (Set all cells to blue)
612617
]);
618+
```
619+
620+
You can define the style of a cell dynamically using a callback. You could then apply one style or another depending on a value.
621+
622+
```php
623+
$rows = [
624+
['name' => 'John Doe', 'age' => '30'],
625+
['name' => 'Jane Smith', 'age' => '25'],
626+
['name' => 'Bob Johnson', 'age' => '40'],
627+
];
613628

614-
// 'head', 'odd', 'even' are all the styles for now
615-
// In future we may support styling a column by its name!
629+
$styles = [
630+
'*:2' => function ($val, $row) {
631+
return $row['age'] >= 30 ? 'boldRed' : '';
632+
},
633+
];
634+
635+
$writer->table($rows, $styles);
616636
```
617637

638+
The example above only processes the cells in the second column of the table. Yf you want to process any cell, you can use the `*:*` key. You could then customise each cell in the table
639+
640+
```php
641+
$rows = [
642+
['name' => 'John Doe', 'age' => '30'],
643+
['name' => 'Jane Smith', 'age' => '25'],
644+
['name' => 'Alice Bob', 'age' => '10'],
645+
['name' => 'Big Johnson', 'age' => '40'],
646+
['name' => 'Jane X', 'age' => '50'],
647+
['name' => 'John Smith', 'age' => '20'],
648+
['name' => 'Bob John', 'age' => '28'],
649+
];
650+
651+
$styles = [
652+
'*:*' => function ($val, $row) {
653+
if ($val === 'Jane X') {
654+
return 'yellow';
655+
}
656+
if ($val == 10 || $val == 20) {
657+
return 'boldPurple';
658+
}
659+
if (str_contains($val, 'Bob')) {
660+
return 'blue';
661+
}
662+
return $row['age'] >= 30 ? 'boldRed' : '';
663+
},
664+
];
665+
666+
$writer->table($rows, $styles);
667+
```
668+
669+
> **Note: Priority in increasing order:**
670+
> - `odd` or `even`
671+
> - `2:*` (row)
672+
> - `*:2` or `b-c <-> column name` (col)
673+
> - `*:*` any cell in table
674+
> - `1:1` (cell) = **highest priority**
675+
618676
#### Justify content (Display setting)
619677

620678
If you want to display certain configurations (from your .env file for example) a bit like Laravel does (via the `php artisan about` command) you can use the `justify` method.

0 commit comments

Comments
 (0)