Skip to content

Commit 5e8937c

Browse files
committed
Merge branch 'Majkie-feature/checked-column-selection' into develop
2 parents 6054b5b + 56f04ab commit 5e8937c

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Traits/WithColumnSelect.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,34 @@ trait WithColumnSelect
1111
{
1212
public bool $columnSelect = false;
1313
public array $columnSelectEnabled = [];
14+
public bool $usesSelect = false;
15+
public bool $rememberColumnSelection = true;
1416

1517
public function mountWithColumnSelect(): void
1618
{
19+
if (! $this->rememberColumnSelection) {
20+
$this->forgetColumnSelectSession();
21+
}
22+
1723
// If the column select is off, make sure to clear the session
1824
if (! $this->columnSelect && session()->has($this->getColumnSelectSessionKey())) {
1925
session()->forget($this->getColumnSelectSessionKey());
2026
}
2127

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+
2233
// Get a list of visible default columns that are not excluded
2334
$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+
})
2542
->map(fn ($column) => $column->column())
2643
->values()
2744
->toArray();
@@ -48,6 +65,11 @@ public function isColumnSelectEnabled($column): bool
4865
return in_array($column instanceof Column ? $column->column() : $column, $this->columnSelectEnabled, true);
4966
}
5067

68+
private function forgetColumnSelectSession(): void
69+
{
70+
session()->forget($this->getColumnSelectSessionKey());
71+
}
72+
5173
private function getColumnSelectSessionKey(): string
5274
{
5375
return $this->tableName.'-columnSelectEnabled';

src/Views/Column.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class Column
7474
*/
7575
public bool $selectable = true;
7676

77+
/**
78+
* @var bool
79+
*/
80+
public bool $selected = false;
81+
7782
/**
7883
* Column constructor.
7984
*
@@ -335,4 +340,22 @@ public function isSelectable(): bool
335340
{
336341
return $this->selectable === true;
337342
}
343+
344+
/**
345+
* @return $this
346+
*/
347+
public function selected(): self
348+
{
349+
$this->selected = true;
350+
351+
return $this;
352+
}
353+
354+
/**
355+
* @return bool
356+
*/
357+
public function isSelected(): bool
358+
{
359+
return $this->selected;
360+
}
338361
}

0 commit comments

Comments
 (0)