Skip to content

Commit

Permalink
check/uncheck all selected columns
Browse files Browse the repository at this point in the history
  • Loading branch information
inmanturbo committed May 18, 2022
1 parent 80783e1 commit 3a03091
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to `laravel-livewire-tables` will be documented in this file

## [Unreleased]

## [column-select-dev] - 2022-05-18

### Added

- Added functionality to select/desect all columns in column selection dropdown

## [column-select-dev] - 2022-05-16

### Added
Expand Down
58 changes: 58 additions & 0 deletions resources/views/components/tools/toolbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,26 @@ class="absolute right-0 z-50 mt-2 w-full bg-white rounded-md divide-y divide-gra
>
<div class="bg-white rounded-md shadow-xs dark:bg-gray-700 dark:text-white">
<div class="p-2" role="menu" aria-orientation="vertical" aria-labelledby="column-select-menu">
<div>
<label
wire:loading.attr="disabled"
class="inline-flex items-center px-2 py-1 disabled:opacity-50 disabled:cursor-wait"
>
<input
class="text-indigo-600 transition duration-150 ease-in-out border-gray-300 rounded shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-gray-900 dark:text-white dark:border-gray-600 dark:hover:bg-gray-600 dark:focus:bg-gray-600 disabled:opacity-50 disabled:cursor-wait"
@if(count($component->selectedColumns) === count($component->getDefaultVisibleColumns()))
checked
wire:click="deselectAllColumns"
@else
unchecked
wire:click="selectAllColumns"
@endif
wire:loading.attr="disabled"
type="checkbox"
/>
<span class="ml-2">{{ __('All Columns') }}</span>
</label>
</div>
@foreach($component->getColumns() as $column)
@if ($column->isVisible() && $column->isSelectable())
<div wire:key="columnSelect-{{ $loop->index }}-{{ $component->getTableName() }}">
Expand Down Expand Up @@ -498,6 +518,25 @@ class="dropdown-menu dropdown-menu-right w-100 mt-0 mt-md-3"
x-bind:class="{'show' : open}"
aria-labelledby="columnSelect-{{ $component->getTableName() }}"
>
<div>
<label
wire:loading.attr="disabled"
class="mb-1"
>
<input
@if(count($component->selectedColumns) === count($component->getDefaultVisibleColumns()))
checked
wire:click="deselectAllColumns"
@else
unchecked
wire:click="selectAllColumns"
@endif
wire:loading.attr="disabled"
type="checkbox"
/>
<span class="ml-2">{{ __('All Columns') }}</span>
</label>
</div>
@foreach($component->getColumns() as $column)
@if ($column->isVisible() && $column->isSelectable())
<div wire:key="columnSelect-{{ $loop->index }}-{{ $component->getTableName() }}">
Expand Down Expand Up @@ -741,6 +780,25 @@ class="dropdown-menu dropdown-menu-end w-100"
x-bind:class="{'show' : open}"
aria-labelledby="columnSelect-{{ $component->getTableName() }}"
>
<div>
<label
wire:loading.attr="disabled"
class="mb-1"
>
<input
@if(count($component->selectedColumns) === count($component->getDefaultVisibleColumns()))
checked
wire:click="deselectAllColumns"
@else
unchecked
wire:click="selectAllColumns"
@endif
wire:loading.attr="disabled"
type="checkbox"
/>
<span class="ml-2">{{ __('All Columns') }}</span>
</label>
</div>
@foreach($component->getColumns() as $column)
@if ($column->isVisible() && $column->isSelectable())
<div wire:key="columnSelect-{{ $loop->index }}-{{ $component->getTableName() }}">
Expand Down
41 changes: 32 additions & 9 deletions src/Traits/WithColumnSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ public function setupColumnSelect(): void
}

// Get a list of visible default columns that are not excluded
$columns = collect($this->getColumns())
->filter(function ($column) {
return $column->isVisible() && $column->isSelectable() && $column->isSelected();
})
->map(fn ($column) => $column->getSlug())
->values()
->toArray();
$columns = $this->getDefaultVisibleColumns();

// Set to either the default set or what is stored in the session
$this->selectedColumns = (isset($this->{$this->tableName}['columns']) && count($this->{$this->tableName}['columns']) > 0) ?
Expand All @@ -49,9 +43,38 @@ public function setupColumnSelect(): void
}
}

public function getDefaultVisibleColumns(): array
{
return collect($this->getColumns())
->filter(function ($column) {
return $column->isVisible() && $column->isSelectable() && $column->isSelected();
})
->map(fn ($column) => $column->getSlug())
->values()
->toArray();
}

public function selectAllColumns()
{
$this->{$this->tableName}['columns'] = [];
$this->forgetColumnSelectSession();
}

public function deselectAllColumns()
{
$this->{$this->tableName}['columns'] = [];
$this->selectedColumns = [];
session([$this->getColumnSelectSessionKey() => []]);
}

public function updatedSelectedColumns(): void
{
$this->{$this->tableName}['columns'] = $this->selectedColumns;
session([$this->getColumnSelectSessionKey() => $this->{$this->tableName}['columns']]);
// The query string isn't needed if it's the same as the default
if(count($this->selectedColumns) === count($this->getDefaultVisibleColumns())){
$this->selectAllColumns();
}else{
$this->{$this->tableName}['columns'] = $this->selectedColumns;
session([$this->getColumnSelectSessionKey() => $this->{$this->tableName}['columns']]);
}
}
}

0 comments on commit 3a03091

Please sign in to comment.