Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 - Column Select Fixes & Tweaks #1360

Merged
merged 24 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
- Fix for Bulk Actions dropdown not working in Bootstrap
- Fix for Column Select "Select All" not consistently updating
- Add fix for lazy loading of table
- Fix for ColumnSelect falling out of sync, displaying unselectable colums, or persisting cols in query that are not selected
- Add setExcludeDeselectedColumnsFromQueryEnabled and setExcludeDeselectedColumnsFromQueryDisabled methods to configure()

## [Unreleased] - 3.x (beta-0)
- Requirements Change
Expand Down
16 changes: 7 additions & 9 deletions resources/views/components/tools/toolbar/bootstrap.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@
<input
wire:loading.attr="disabled"
type="checkbox"
@if($component->allDefaultVisibleColumnsAreSelected()) checked wire:click="deselectAllColumns" @else unchecked wire:click="selectAllColumns" @endif
@if($component->getSelectableSelectedColumns()->count() == $component->getSelectableColumns()->count()) checked wire:click="deselectAllColumns" @else unchecked wire:click="selectAllColumns" @endif
/>

<span class="ml-2">{{ __('All Columns') }}</span>
Expand All @@ -277,7 +277,7 @@
wire:loading.attr="disabled"
type="checkbox"
class="form-check-input"
@if($component->allDefaultVisibleColumnsAreSelected()) checked wire:click="deselectAllColumns" @else unchecked wire:click="selectAllColumns" @endif
@if($component->getSelectableSelectedColumns()->count() == $component->getSelectableColumns()->count()) checked wire:click="deselectAllColumns" @else unchecked wire:click="selectAllColumns" @endif
/>

<label wire:loading.attr="disabled" class="form-check-label">
Expand All @@ -286,8 +286,7 @@ class="form-check-input"
</div>
@endif

@foreach ($component->getColumns() as $column)
@if ($column->isVisible() && $column->isSelectable())
@foreach ($component->getColumnsForColumnSelect() as $columnSlug => $columnTitle)
<div
wire:key="{{ $tableName }}-columnSelect-{{ $loop->index }}"
@class([
Expand All @@ -304,10 +303,10 @@ class="px-2 {{ $loop->last ? 'mb-0' : 'mb-1' }}"
wire:model.live="selectedColumns"
wire:target="selectedColumns"
wire:loading.attr="disabled" type="checkbox"
value="{{ $column->getSlug() }}"
value="{{ $columnSlug }}"
/>
<span class="ml-2">
{{ $column->getTitle() }}
{{ $columnTitle }}
</span>
</label>
@elseif($component->isBootstrap5())
Expand All @@ -317,18 +316,17 @@ class="px-2 {{ $loop->last ? 'mb-0' : 'mb-1' }}"
wire:loading.attr="disabled"
type="checkbox"
class="form-check-input"
value="{{ $column->getSlug() }}"
value="{{ $columnSlug }}"
/>
<label
wire:loading.attr="disabled"
wire:target="selectedColumns"
class="{{ $loop->last ? 'mb-0' : 'mb-1' }} form-check-label"
>
{{ $column->getTitle() }}
{{ $columnTitle }}
</label>
@endif
</div>
@endif
@endforeach
</div>
</div>
Expand Down
12 changes: 5 additions & 7 deletions resources/views/components/tools/toolbar/tailwind.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,14 @@ class="inline-flex items-center px-2 py-1 disabled:opacity-50 disabled:cursor-wa
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"
wire:loading.attr="disabled"
type="checkbox"
@checked($component->visibleColumnCount == $component->defaultVisibleColumnCount)
@if($component->visibleColumnCount >= $component->defaultVisibleColumnCount) wire:click="deselectAllColumns" @else wire:click="selectAllColumns" @endif
@checked($component->getSelectableSelectedColumns()->count() == $component->getSelectableColumns()->count())
@if($component->getSelectableSelectedColumns()->count() == $component->getSelectableColumns()->count()) wire:click="deselectAllColumns" @else wire:click="selectAllColumns" @endif
>
<span class="ml-2">{{ __('All Columns') }}</span>
</label>
</div>

@foreach ($component->getColumns() as $column)
@if ($column->isVisible() && $column->isSelectable())
@foreach ($component->getColumnsForColumnSelect() as $columnSlug => $columnTitle)
<div
wire:key="{{ $tableName }}-columnSelect-{{ $loop->index }}"
>
Expand All @@ -268,11 +267,10 @@ class="inline-flex items-center px-2 py-1 disabled:opacity-50 disabled:cursor-wa
class="text-indigo-600 rounded border-gray-300 shadow-sm transition duration-150 ease-in-out 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"
wire:model.live="selectedColumns" wire:target="selectedColumns"
wire:loading.attr="disabled" type="checkbox"
value="{{ $column->getSlug() }}" />
<span class="ml-2">{{ $column->getTitle() }}</span>
value="{{ $columnSlug }}" />
<span class="ml-2">{{ $columnTitle }}</span>
</label>
</div>
@endif
@endforeach
</div>
</div>
Expand Down
50 changes: 50 additions & 0 deletions src/Traits/Configuration/ColumnSelectConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rappasoft\LaravelLivewireTables\Traits\Configuration;

use Rappasoft\LaravelLivewireTables\Views\Column;

trait ColumnSelectConfiguration
{
public function setColumnSelectStatus(bool $status): self
Expand Down Expand Up @@ -45,4 +47,52 @@ public function setRememberColumnSelectionDisabled(): self

return $this;
}

public function setExcludeDeselectedColumnsFromQueryEnabled(): self
{
$this->setExcludeDeselectedColumnsFromQuery(true);

return $this;
}

public function setExcludeDeselectedColumnsFromQueryDisabled(): self
{
$this->setExcludeDeselectedColumnsFromQuery(false);

return $this;
}

public function setExcludeDeselectedColumnsFromQuery(bool $status): self
{
$this->excludeDeselectedColumnsFromQuery = $status;

return $this;
}

public function setColumnSelectHiddenOnMobile(): self
{
$this->columnSelectHiddenOnMobile = true;

return $this;
}

public function setColumnSelectHiddenOnTablet(): self
{
$this->columnSelectHiddenOnTablet = true;

return $this;
}

public function setDefaultDeselectedColumns(): array
{
return collect($this->getColumns()
->reject(fn (Column $column) => ! $column->isSelectable())
->reject(fn (Column $column) => $column->isSelectable() && $column->isSelected())
)
->keyBy(function (Column $column, int $key) {
return $column->getSlug();
})
->map(fn ($column) => $column->getTitle())
->toArray();
}
}
33 changes: 0 additions & 33 deletions src/Traits/Helpers/ColumnHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,39 +91,6 @@ public function getColumnRelationStrings(): array
->toArray();
}

public function getCurrentlySelectedCols()
{

$this->defaultVisibleColumnCount = count($this->getDefaultVisibleColumns());
$this->visibleColumnCount = count(array_intersect($this->selectedColumns, $this->getDefaultVisibleColumns()));

}

public function getReallySelectedColumns(): array
{
return $this->getColumns()
->reject(fn (Column $column) => $column->isLabel())
->reject(fn (Column $column) => ! $column->isSelected())
->values()
->toArray();
}

public function getSelectableColumns(): Collection
{
return $this->getColumns()
->reject(fn (Column $column) => $column->isLabel())
->reject(fn (Column $column) => ! $column->isSelectable())
->values();
}

public function getCurrentlySelectedColumns(): Collection
{
return $this->getColumns()
->reject(fn (Column $column) => $column->isLabel())
->reject(fn (Column $column) => ! $column->isSelectable())
->values();
}

public function getSearchableColumns(): Collection
{
return $this->getColumns()
Expand Down
95 changes: 85 additions & 10 deletions src/Traits/Helpers/ColumnSelectHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rappasoft\LaravelLivewireTables\Traits\Helpers;

use Illuminate\Support\Collection;
use Rappasoft\LaravelLivewireTables\Views\Column;

trait ColumnSelectHelpers
Expand Down Expand Up @@ -51,27 +52,101 @@ protected function getColumnSelectSessionKey(): string
return $this->getDataTableFingerprint().'-columnSelectEnabled';
}

public function setColumnSelectHiddenOnMobile(): self
public function getColumnSelectIsHiddenOnTablet(): bool
{
$this->columnSelectHiddenOnMobile = true;
return $this->columnSelectHiddenOnTablet;
}

return $this;
public function getExcludeDeselectedColumnsFromQuery(): bool
{
return $this->excludeDeselectedColumnsFromQuery;
}

public function getColumnSelectIsHiddenOnTablet(): bool
public function getColumnSelectIsHiddenOnMobile(): bool
{
return $this->columnSelectHiddenOnTablet;
return $this->columnSelectHiddenOnMobile;
}

public function setColumnSelectHiddenOnTablet(): self
public function getSelectableColumns(): Collection
{
$this->columnSelectHiddenOnTablet = true;
return $this->getColumns()
->reject(fn (Column $column) => $column->isHidden())
->reject(fn (Column $column) => ! $column->isSelectable())
->values();
}

return $this;
public function getSelectableSelectedColumns(): Collection
{
return $this->getColumns()
->reject(fn (Column $column) => $column->isHidden())
->reject(fn (Column $column) => ! $column->isSelectable())
->reject(fn (Column $column) => ! $this->columnSelectIsEnabledForColumn($column))
->values();
}

public function getColumnSelectIsHiddenOnMobile(): bool
public function getCurrentlySelectedCols(): void
{
return $this->columnSelectHiddenOnMobile;
$this->defaultVisibleColumnCount = count($this->getDefaultVisibleColumns());
$this->visibleColumnCount = count(array_intersect($this->selectedColumns, $this->getDefaultVisibleColumns()));
}

public function getColsForData(): Collection
{
$selectableCols = $this->getSelectableColumns();
$unSelectableCols = $this->getUnSelectableColumns();

return $selectableCols->merge($unSelectableCols);
}

public function getUnSelectableColumns(): Collection
{
return $this->getColumns()
->reject(fn (Column $column) => $column->isHidden())
->reject(fn (Column $column) => $column->isSelectable())
->values();
}

public function getSelectedColumns(): array
{
return $this->selectedColumns ?? [];
}

public function getSelectedColumnsForQuery(): array
{
return $this->getColumns()
->reject(fn (Column $column) => $column->isLabel())
->reject(fn (Column $column) => $column->isHidden())
->reject(fn (Column $column) => ($column->isSelectable() && ! $this->columnSelectIsEnabledForColumn($column)))
->values()
->toArray();
}

public function getColumnsForColumnSelect(): array
{
return $this->getColumns()
->reject(fn (Column $column) => ! $column->isSelectable())
->reject(fn (Column $column) => $column->isHidden())
->keyBy(function (Column $column, int $key) {
return $column->getSlug();
})
->map(fn ($column) => $column->getTitle())
->toArray();
}

public function getDefaultVisibleColumns(): array
{
return collect($this->getColumns()
->reject(fn (Column $column) => $column->isHidden())
->reject(fn (Column $column) => $column->isSelectable() && ! $column->isSelected())

)
->map(fn ($column) => $column->getSlug())
->values()
->toArray();
}

public function getAllColumnsAreSelected(): bool
{
return $this->getSelectableSelectedColumns()->count() === $this->getSelectableColumns()->count();
}
}
Loading