Skip to content

Commit 4f1c076

Browse files
committed
Merge branch 'feature/checked-column-selection' of https://github.com/Majkie/laravel-livewire-tables into Majkie-feature/checked-column-selection
2 parents ffd23f4 + 15f7dc8 commit 4f1c076

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Traits/WithColumnSelect.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,28 @@ trait WithColumnSelect
1212
public bool $columnSelect = false;
1313
public array $columnSelectEnabled = [];
1414

15+
public bool $usesSelect = false;
16+
1517
public function mountWithColumnSelect(): void
1618
{
1719
// If the column select is off, make sure to clear the session
1820
if (! $this->columnSelect && session()->has($this->getColumnSelectSessionKey())) {
1921
session()->forget($this->getColumnSelectSessionKey());
2022
}
2123

24+
$selected = collect($this->columns())
25+
->filter(fn ($column) => $column->isSelected())->count();
26+
27+
if ($selected > 0) $this->usesSelect = true;
28+
2229
// Get a list of visible default columns that are not excluded
2330
$columns = collect($this->columns())
24-
->filter(fn ($column) => $column->isVisible() && $column->isSelectable())
31+
->filter(function ($column) {
32+
if ($this->usesSelect) {
33+
return $column->isVisible() && $column->isSelectable() && $column->isSelected();
34+
}
35+
return $column->isVisible() && $column->isSelectable();
36+
})
2537
->map(fn ($column) => $column->column())
2638
->values()
2739
->toArray();

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)