@@ -11,17 +11,34 @@ trait WithColumnSelect
11
11
{
12
12
public bool $ columnSelect = false ;
13
13
public array $ columnSelectEnabled = [];
14
+ public bool $ usesSelect = false ;
15
+ public bool $ rememberColumnSelection = true ;
14
16
15
17
public function mountWithColumnSelect (): void
16
18
{
19
+ if (! $ this ->rememberColumnSelection ) {
20
+ $ this ->forgetColumnSelectSession ();
21
+ }
22
+
17
23
// If the column select is off, make sure to clear the session
18
24
if (! $ this ->columnSelect && session ()->has ($ this ->getColumnSelectSessionKey ())) {
19
25
session ()->forget ($ this ->getColumnSelectSessionKey ());
20
26
}
21
27
28
+ // If any of the columns are user selected
29
+ if (collect ($ this ->columns ())->filter (fn ($ column ) => $ column ->isSelected ())->count () > 0 ) {
30
+ $ this ->usesSelect = true ;
31
+ }
32
+
22
33
// Get a list of visible default columns that are not excluded
23
34
$ columns = collect ($ this ->columns ())
24
- ->filter (fn ($ column ) => $ column ->isVisible () && $ column ->isSelectable ())
35
+ ->filter (function ($ column ) {
36
+ if ($ this ->usesSelect ) {
37
+ return $ column ->isVisible () && $ column ->isSelectable () && $ column ->isSelected ();
38
+ }
39
+
40
+ return $ column ->isVisible () && $ column ->isSelectable ();
41
+ })
25
42
->map (fn ($ column ) => $ column ->column ())
26
43
->values ()
27
44
->toArray ();
@@ -48,6 +65,11 @@ public function isColumnSelectEnabled($column): bool
48
65
return in_array ($ column instanceof Column ? $ column ->column () : $ column , $ this ->columnSelectEnabled , true );
49
66
}
50
67
68
+ private function forgetColumnSelectSession (): void
69
+ {
70
+ session ()->forget ($ this ->getColumnSelectSessionKey ());
71
+ }
72
+
51
73
private function getColumnSelectSessionKey (): string
52
74
{
53
75
return $ this ->tableName .'-columnSelectEnabled ' ;
0 commit comments