@@ -13,14 +13,19 @@ trait ColumnHelpers
1313 */
1414 public function setColumns (): void
1515 {
16- $ this ->prependedColumns = $ this ->getPrependedColumns ();
16+ $ columns = collect ($ this ->getPrependedColumns ())->concat ($ this ->columns ())->concat (collect ($ this ->getAppendedColumns ()));
17+ $ this ->columns = $ columns ->filter (fn ($ column ) => $ column instanceof Column);
18+ }
1719
18- $ columns = collect ($ this ->columns ())
20+ protected function setupColumns (): void
21+ {
22+ $ this ->columns = $ this ->columns
1923 ->filter (fn ($ column ) => $ column instanceof Column)
2024 ->map (function (Column $ column ) {
21- $ column ->setTheme ($ this ->getTheme ());
22- $ column ->setHasTableRowUrl ($ this ->hasTableRowUrl ());
23- $ column ->setIsReorderColumn ($ this ->getDefaultReorderColumn () == $ column ->getField ());
25+ $ column ->setTheme ($ this ->getTheme ())
26+ ->setHasTableRowUrl ($ this ->hasTableRowUrl ())
27+ ->setIsReorderColumn ($ this ->getDefaultReorderColumn () == $ column ->getField ());
28+
2429 if ($ column instanceof AggregateColumn) {
2530 if ($ column ->getAggregateMethod () == 'count ' && $ column ->hasDataSource ()) {
2631 $ this ->addExtraWithCount ($ column ->getDataSource ());
@@ -42,13 +47,15 @@ public function setColumns(): void
4247 return $ column ;
4348 });
4449
45- $ this ->appendedColumns = $ this ->getAppendedColumns ();
46-
47- $ this ->columns = collect ([...$ this ->prependedColumns , ...$ columns , ...$ this ->appendedColumns ]);
50+ $ this ->hasRunColumnSetup = true ;
4851 }
4952
5053 public function getColumns (): Collection
5154 {
55+ if (! $ this ->hasRunColumnSetup ) {
56+ $ this ->setupColumns ();
57+ }
58+
5259 return $ this ->columns ;
5360 }
5461
@@ -206,63 +213,12 @@ public function getColspanCount(): int
206213
207214 public function getPrependedColumns (): Collection
208215 {
209- return collect ($ this ->prependedColumns ?? $ this ->prependColumns ())
210- ->filter (fn ($ column ) => $ column instanceof Column)
211- ->map (function (Column $ column ) {
212- $ column ->setTheme ($ this ->getTheme ());
213- $ column ->setHasTableRowUrl ($ this ->hasTableRowUrl ());
214- $ column ->setIsReorderColumn ($ this ->getDefaultReorderColumn () == $ column ->getField ());
215- if ($ column instanceof AggregateColumn) {
216- if ($ column ->getAggregateMethod () == 'count ' && $ column ->hasDataSource ()) {
217- $ this ->addExtraWithCount ($ column ->getDataSource ());
218- } elseif ($ column ->getAggregateMethod () == 'sum ' && $ column ->hasDataSource () && $ column ->hasForeignColumn ()) {
219- $ this ->addExtraWithSum ($ column ->getDataSource (), $ column ->getForeignColumn ());
220- } elseif ($ column ->getAggregateMethod () == 'avg ' && $ column ->hasDataSource () && $ column ->hasForeignColumn ()) {
221- $ this ->addExtraWithAvg ($ column ->getDataSource (), $ column ->getForeignColumn ());
222- }
223- }
224-
225- if ($ column ->hasField ()) {
226- if ($ column ->isBaseColumn ()) {
227- $ column ->setTable ($ this ->getBuilder ()->getModel ()->getTable ());
228- } else {
229- $ column ->setTable ($ this ->getTableForColumn ($ column ));
230- }
231- }
232-
233- return $ column ;
234- });
216+ return $ this ->prependedColumns ?? collect ($ this ->prependColumns ());
235217 }
236218
237219 public function getAppendedColumns (): Collection
238220 {
239- return collect ($ this ->appendedColumns ?? $ this ->appendColumns ())
240- ->filter (fn ($ column ) => $ column instanceof Column)
241- ->map (function (Column $ column ) {
242- $ column ->setTheme ($ this ->getTheme ());
243- $ column ->setHasTableRowUrl ($ this ->hasTableRowUrl ());
244- $ column ->setIsReorderColumn ($ this ->getDefaultReorderColumn () == $ column ->getField ());
245- if ($ column instanceof AggregateColumn) {
246- if ($ column ->getAggregateMethod () == 'count ' && $ column ->hasDataSource ()) {
247- $ this ->addExtraWithCount ($ column ->getDataSource ());
248- } elseif ($ column ->getAggregateMethod () == 'sum ' && $ column ->hasDataSource () && $ column ->hasForeignColumn ()) {
249- $ this ->addExtraWithSum ($ column ->getDataSource (), $ column ->getForeignColumn ());
250- } elseif ($ column ->getAggregateMethod () == 'avg ' && $ column ->hasDataSource () && $ column ->hasForeignColumn ()) {
251- $ this ->addExtraWithAvg ($ column ->getDataSource (), $ column ->getForeignColumn ());
252- }
253- }
254-
255- if ($ column ->hasField ()) {
256- if ($ column ->isBaseColumn ()) {
257- $ column ->setTable ($ this ->getBuilder ()->getModel ()->getTable ());
258- } else {
259- $ column ->setTable ($ this ->getTableForColumn ($ column ));
260- }
261- }
262-
263- return $ column ;
264- });
265-
221+ return $ this ->appendedColumns ?? collect ($ this ->appendColumns ());
266222 }
267223
268224 public function getCollapsedAlwaysColumns (): Collection
@@ -287,4 +243,20 @@ public function shouldCollapseAlways(): bool
287243
288244 return $ this ->shouldAlwaysCollapse ;
289245 }
246+
247+ /**
248+ * Prepend columns.
249+ */
250+ public function prependColumns (): array
251+ {
252+ return [];
253+ }
254+
255+ /**
256+ * Append columns.
257+ */
258+ public function appendColumns (): array
259+ {
260+ return [];
261+ }
290262}
0 commit comments