Skip to content

Commit

Permalink
Merge branch 'feature/checked-column-selection' of https://github.com…
Browse files Browse the repository at this point in the history
…/Majkie/laravel-livewire-tables into Majkie-feature/checked-column-selection
  • Loading branch information
rappasoft committed Sep 1, 2021
2 parents ffd23f4 + 15f7dc8 commit 4f1c076
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Traits/WithColumnSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,28 @@ trait WithColumnSelect
public bool $columnSelect = false;
public array $columnSelectEnabled = [];

public bool $usesSelect = false;

public function mountWithColumnSelect(): void
{
// If the column select is off, make sure to clear the session
if (! $this->columnSelect && session()->has($this->getColumnSelectSessionKey())) {
session()->forget($this->getColumnSelectSessionKey());
}

$selected = collect($this->columns())
->filter(fn ($column) => $column->isSelected())->count();

if ($selected > 0) $this->usesSelect = true;

// Get a list of visible default columns that are not excluded
$columns = collect($this->columns())
->filter(fn ($column) => $column->isVisible() && $column->isSelectable())
->filter(function ($column) {
if ($this->usesSelect) {
return $column->isVisible() && $column->isSelectable() && $column->isSelected();
}
return $column->isVisible() && $column->isSelectable();
})
->map(fn ($column) => $column->column())
->values()
->toArray();
Expand Down
23 changes: 23 additions & 0 deletions src/Views/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class Column
*/
public bool $selectable = true;

/**
* @var bool
*/
public bool $selected = false;

/**
* Column constructor.
*
Expand Down Expand Up @@ -335,4 +340,22 @@ public function isSelectable(): bool
{
return $this->selectable === true;
}

/**
* @return $this
*/
public function selected(): self
{
$this->selected = true;

return $this;
}

/**
* @return bool
*/
public function isSelected(): bool
{
return $this->selected;
}
}

0 comments on commit 4f1c076

Please sign in to comment.