Skip to content

Commit 4d68332

Browse files
committed
Move exclusion to Column component
1 parent d66063b commit 4d68332

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

resources/views/tailwind/includes/column-select.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg z-50"
4040
<div class="rounded-md bg-white shadow-xs">
4141
<div class="p-2" role="menu" aria-orientation="vertical" aria-labelledby="column-select-menu">
4242
@foreach($columns as $column)
43-
@if ($column->isVisible() && ! $this->isColumnSelectExcluded($column))
43+
@if ($column->isVisible() && $column->isSelectable())
4444
<div>
4545
<label class="px-2 py-1 inline-flex items-center">
4646
<input wire:model="columnSelectEnabled" type="checkbox" value="{{ $column->column() }}">

src/Traits/WithColumnSelect.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
trait WithColumnSelect
1111
{
1212
public bool $columnSelect = false;
13-
public array $columnSelectExcluded = [];
1413
public array $columnSelectEnabled = [];
1514

1615
public function mountWithColumnSelect(): void
@@ -22,7 +21,7 @@ public function mountWithColumnSelect(): void
2221

2322
// Get a list of visible default columns that are not excluded
2423
$columns = collect($this->columns())
25-
->filter(fn ($column) => $column->isVisible() && ! $this->isColumnSelectExcluded($column))
24+
->filter(fn ($column) => $column->isVisible() && $column->isSelectable())
2625
->map(fn ($column) => $column->column())
2726
->values()
2827
->toArray();
@@ -31,9 +30,10 @@ public function mountWithColumnSelect(): void
3130
$this->columnSelectEnabled = session()->get($this->tableName.'-columnSelectEnabled', $columns);
3231

3332
// Check to see if there are any excluded that are already stored in the enabled and remove them
34-
foreach ($this->columnSelectExcluded as $column) {
35-
if (! in_array($column, $this->columnSelectEnabled, true)) {
36-
session([$this->tableName.'-columnSelectEnabled' => $this->columnSelectEnabled[] = $column]);
33+
foreach ($this->columns() as $column) {
34+
if (! $column->isSelectable() && ! in_array($column->column(), $this->columnSelectEnabled, true)) {
35+
$this->columnSelectEnabled[] = $column->column();
36+
session([$this->tableName.'-columnSelectEnabled' => $this->columnSelectEnabled]);
3737
}
3838
}
3939
}
@@ -47,9 +47,4 @@ public function isColumnSelectEnabled($column): bool
4747
{
4848
return in_array($column instanceof Column ? $column->column() : $column, $this->columnSelectEnabled, true);
4949
}
50-
51-
public function isColumnSelectExcluded($column): bool
52-
{
53-
return in_array($column instanceof Column ? $column->column() : $column, $this->columnSelectExcluded, true);
54-
}
5550
}

src/Views/Column.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ class Column
6464
*/
6565
public bool $hidden = false;
6666

67+
/**
68+
* @var bool
69+
*/
70+
public bool $selectable = true;
71+
6772
/**
6873
* Column constructor.
6974
*
@@ -287,4 +292,22 @@ public function isVisible(): bool
287292
{
288293
return $this->hidden !== true;
289294
}
295+
296+
/**
297+
* @return $this
298+
*/
299+
public function excludeFromSelectable(): self
300+
{
301+
$this->selectable = false;
302+
303+
return $this;
304+
}
305+
306+
/**
307+
* @return bool
308+
*/
309+
public function isSelectable(): bool
310+
{
311+
return $this->selectable === true;
312+
}
290313
}

0 commit comments

Comments
 (0)