77use ACTTraining \QueryBuilder \Support \Columns \DateColumn ;
88use ACTTraining \QueryBuilder \Support \Conditions \BooleanCondition ;
99use ACTTraining \QueryBuilder \Support \Conditions \DateCondition ;
10+ use ACTTraining \QueryBuilder \Support \Conditions \NumberCondition ;
1011use ACTTraining \QueryBuilder \Support \Conditions \TextCondition ;
1112
1213trait WithReportBuilder
1314{
1415 public array $ selectedColumns = [];
1516
16- public function updatedSelectedColumns ($ value ): void
17+ private function findElementByKey (array $ array , $ targetValue ): ?array
18+ {
19+ foreach ($ array as $ value ) {
20+ // Check if the current item is an array
21+ if (is_array ($ value )) {
22+ // Check if it contains the 'key' element with the target value
23+ if (isset ($ value ['key ' ]) && $ value ['key ' ] === $ targetValue ) {
24+ return $ value ; // Return the found item
25+ }
26+
27+ // Recursively search the sub-array
28+ $ result = $ this ->findElementByKey ($ value , $ targetValue );
29+ if ($ result !== null ) {
30+ return $ result ;
31+ }
32+ }
33+ }
34+
35+ return null ; // Return null if no match is found
36+ }
37+
38+ public function updatedSelectedColumns (): void
1739 {
1840 $ this ->resetPage ();
19- ray ( $ value );
41+ $ this -> dispatch ( ' refreshTable ' )-> self ( );
2042 }
2143
2244 public function availableColumns (): array
@@ -26,7 +48,12 @@ public function availableColumns(): array
2648
2749 public function configuredColumns (): array
2850 {
29- return [];
51+ $ columns = [];
52+
53+ foreach ($ this ->selectedColumns as $ column ) {
54+ $ columns [] = $ this ->findElementByKey ($ this ->availableColumns (), $ column );
55+ }
56+ return $ columns ;
3057 }
3158
3259 public function buildColumns (): array
@@ -37,6 +64,7 @@ public function buildColumns(): array
3764
3865 foreach ($ this ->configuredColumns () as $ column ) {
3966 $ columnToAdd = match ($ column ['type ' ] ?? null ) {
67+ 'number ' => Column::make ($ column ['label ' ], $ column ['key ' ])->justify ('right ' ),
4068 'boolean ' => BooleanColumn::make ($ column ['label ' ], $ column ['key ' ])->justify ('center ' )->hideIf (false ),
4169 'date ' => DateColumn::make ($ column ['label ' ], $ column ['key ' ])->format (config ('settings.date.short-format ' ))->justify ('right ' ),
4270 default => Column::make ($ column ['label ' ], $ column ['key ' ])
@@ -48,6 +76,10 @@ public function buildColumns(): array
4876 $ columnToAdd ->component ('columns.common.title ' );
4977 }
5078
79+ if ($ column ['sortable ' ] ?? false ) {
80+ $ columnToAdd ->sortable ();
81+ }
82+
5183 $ columns [] = $ columnToAdd ;
5284
5385 $ counter ++;
@@ -64,6 +96,7 @@ public function buildConditions(): array
6496 continue ;
6597 }
6698 $ conditions [] = match ($ column ['type ' ] ?? null ) {
99+ 'number ' => NumberCondition::make ($ column ['label ' ], $ column ['key ' ]),
67100 'boolean ' => BooleanCondition::make ($ column ['label ' ], $ column ['key ' ]),
68101 'date ' => DateCondition::make ($ column ['label ' ], $ column ['key ' ]),
69102 default => TextCondition::make ($ column ['label ' ], $ column ['key ' ])
0 commit comments