Skip to content

Commit 48fbb3a

Browse files
committed
Column Select Core
- Add column select trait - Add needed functionality to show/hide columns
1 parent 8ff083d commit 48fbb3a

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

resources/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"Applied Sorting": "Applied Sorting",
55
"Bulk Actions": "Bulk Actions",
66
"Clear": "Clear",
7+
"Columns": "Columns",
78
"Filters": "Filters",
89
"Remove filter option": "Remove filter option",
910
"Remove sort option": "Remove sort option",

src/DataTableComponent.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Collection;
99
use Livewire\Component;
1010
use Rappasoft\LaravelLivewireTables\Traits\WithBulkActions;
11+
use Rappasoft\LaravelLivewireTables\Traits\WithColumnSelect;
1112
use Rappasoft\LaravelLivewireTables\Traits\WithCustomPagination;
1213
use Rappasoft\LaravelLivewireTables\Traits\WithFilters;
1314
use Rappasoft\LaravelLivewireTables\Traits\WithPerPagePagination;
@@ -22,6 +23,7 @@
2223
abstract class DataTableComponent extends Component
2324
{
2425
use WithBulkActions;
26+
use WithColumnSelect;
2527
use WithCustomPagination;
2628
use WithFilters;
2729
use WithPerPagePagination;

src/Traits/WithColumnSelect.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits;
4+
5+
use Rappasoft\LaravelLivewireTables\Views\Column;
6+
7+
/**
8+
* Trait WithColumnSelect.
9+
*/
10+
trait WithColumnSelect
11+
{
12+
public bool $columnSelect = false;
13+
public array $columnSelectExcluded = [];
14+
public array $columnSelectEnabled = [];
15+
16+
public function mountWithColumnSelect(): void
17+
{
18+
// If the column select is off, make sure to clear the session
19+
if (! $this->columnSelect && session()->has($this->tableName.'-columnSelectEnabled')) {
20+
session()->forget($this->tableName.'-columnSelectEnabled');
21+
}
22+
23+
// Get a list of visible default columns that are not excluded
24+
$columns = collect($this->columns())
25+
->filter(fn ($column) => $column->isVisible() && ! $this->isColumnSelectExcluded($column))
26+
->map(fn ($column) => $column->column())
27+
->values()
28+
->toArray();
29+
30+
// Set to either the default set or what is stored in the session
31+
$this->columnSelectEnabled = session()->get($this->tableName.'-columnSelectEnabled', $columns);
32+
33+
// 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]);
37+
}
38+
}
39+
}
40+
41+
public function updatedColumnSelectEnabled(): void
42+
{
43+
session([$this->tableName.'-columnSelectEnabled' => $this->columnSelectEnabled]);
44+
}
45+
46+
public function isColumnSelectEnabled($column): bool
47+
{
48+
return in_array($column instanceof Column ? $column->column() : $column, $this->columnSelectEnabled, true);
49+
}
50+
51+
public function isColumnSelectExcluded($column): bool
52+
{
53+
return in_array($column instanceof Column ? $column->column() : $column, $this->columnSelectExcluded, true);
54+
}
55+
}

0 commit comments

Comments
 (0)