@@ -50,6 +50,9 @@ class Table
50
50
private array $ columnStyles = [];
51
51
private array $ columnWidths = [];
52
52
private array $ columnMaxWidths = [];
53
+ private array $ optionalColumns = [];
54
+ private array $ droppedColumns = [];
55
+ private int $ maxWidth = 0 ;
53
56
private bool $ rendered = false ;
54
57
private string $ displayOrientation = self ::DISPLAY_ORIENTATION_DEFAULT ;
55
58
@@ -174,6 +177,18 @@ public function setColumnMaxWidth(int $columnIndex, int $width): static
174
177
return $ this ;
175
178
}
176
179
180
+ public function setOptionalColumns (array $ columns ): static
181
+ {
182
+ $ this ->optionalColumns = $ columns ;
183
+ return $ this ;
184
+ }
185
+
186
+ public function setMaxWidth (int $ maxWidth ): static
187
+ {
188
+ $ this ->maxWidth = $ maxWidth ;
189
+ return $ this ;
190
+ }
191
+
177
192
/**
178
193
* @return $this
179
194
*/
@@ -402,6 +417,16 @@ public function render()
402
417
continue ;
403
418
}
404
419
420
+ if ($ this ->droppedColumns ) {
421
+ foreach ($ this ->droppedColumns as $ column ) {
422
+ if ($ this ->numberOfColumns < count ($ row )) {
423
+ unset($ row [$ column ]);
424
+ }
425
+ }
426
+
427
+ $ row = array_values ($ row );
428
+ }
429
+
405
430
if ($ isHeader && !$ isHeaderSeparatorRendered ) {
406
431
$ this ->renderRowSeparator (
407
432
$ isHeader ? self ::SEPARATOR_TOP : self ::SEPARATOR_TOP_BOTTOM ,
@@ -815,6 +840,36 @@ private function calculateColumnsWidth(iterable $groups)
815
840
816
841
$ this ->effectiveColumnWidths [$ column ] = max ($ lengths ) + Helper::width ($ this ->style ->getCellRowContentFormat ()) - 2 ;
817
842
}
843
+
844
+ $ effectiveColumnWidths = $ this ->effectiveColumnWidths ;
845
+
846
+ for ($ column = $ this ->numberOfColumns ; $ this ->numberOfColumns > 0 ; --$ column ) {
847
+ if ($ this ->maxWidth && $ this ->maxWidth > array_sum ($ effectiveColumnWidths )) {
848
+ break ;
849
+ }
850
+
851
+ $ droppedColumn = array_pop ($ this ->optionalColumns );
852
+ unset($ effectiveColumnWidths [$ droppedColumn ]);
853
+ $ this ->droppedColumns [] = $ droppedColumn ;
854
+ }
855
+
856
+ if ($ this ->droppedColumns ) {
857
+ foreach ($ this ->droppedColumns as $ column ) {
858
+ unset(
859
+ $ this ->effectiveColumnWidths [$ column ],
860
+ $ this ->columnMaxWidths [$ column ],
861
+ $ this ->columnStyles [$ column ],
862
+ $ this ->columnWidths [$ column ],
863
+ );
864
+ }
865
+
866
+ $ this ->effectiveColumnWidths = array_values ($ this ->effectiveColumnWidths );
867
+ $ this ->columnMaxWidths = array_values ($ this ->columnMaxWidths );
868
+ $ this ->columnStyles = array_values ($ this ->columnStyles );
869
+ $ this ->columnWidths = array_values ($ this ->columnWidths );
870
+
871
+ $ this ->numberOfColumns -= count ($ this ->droppedColumns );
872
+ }
818
873
}
819
874
820
875
private function getColumnSeparatorWidth (): int
0 commit comments