You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+60-2Lines changed: 60 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -609,12 +609,70 @@ $writer->table([
609
609
'head' => 'boldGreen', // For the table heading
610
610
'odd' => 'bold', // For the odd rows (1st row is odd, then 3, 5 etc)
611
611
'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)
612
617
]);
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
+
];
613
628
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);
616
636
```
617
637
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
+
618
676
#### Justify content (Display setting)
619
677
620
678
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