Skip to content

Commit 7d2a8c8

Browse files
authored
Merge pull request #776 from rappasoft/column-select
Column select
2 parents 55ec365 + 80b3473 commit 7d2a8c8

18 files changed

+224
-37
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
44

55
## [Unreleased]
66

7+
## [column-select-dev] - 2022-05-16
8+
9+
### Added
10+
11+
- Added functionality to bookmark or deep link column selection
12+
- Added functionality to identify different datatable components as unique in column selection
13+
- Added Funcitonality to configure query string alias
14+
- Added Funcitonality to configure session key for column selection (dataTableFingerprint)
15+
716
## [2.7.0] - 2022-05-07
817

918
### Added

docs/columns/column-selection.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,19 @@ public function configure(): void
9595
// Shorthand for $this->setRememberColumnSelectionStatus(false)
9696
$this->setRememberColumnSelectionDisabled();
9797
}
98-
```
98+
```
99+
100+
### setDataTableFingerprint
101+
102+
In order to idenfify each table and prevent conflicts on column selection, each table is given a unique fingerprint.
103+
This fingerprint is generated using the static::class name of the component. If you are reusing
104+
the same component in different parts of your application, you may need to set your own custom fingerprint.
105+
106+
```php
107+
public function configure(): void
108+
{
109+
// Default fingerprint is output of protected method dataTableFingerprint()
110+
// Below will prepend the current route name
111+
$this->setDataTableFingerprint(route()->getName() . '-' . $this->dataTableFingerprint());
112+
}
113+
```

docs/misc/multiple-tables.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,14 @@ public function configure(): void
9292
$this->setQueryStringDisabled();
9393
}
9494
```
95+
96+
## Disabling column selection for multiple of the same component
97+
98+
You should also disable the columns selection for those components so the column selection state does not get replaced by one or the other:
99+
100+
```php
101+
public function configure(): void
102+
{
103+
$this->setColumnSelectStatus(false);
104+
}
105+
```

resources/views/components/tools/toolbar.blade.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class="text-indigo-600 rounded border-gray-300 shadow-sm transition duration-150
260260
wire:target="selectedColumns"
261261
wire:loading.attr="disabled"
262262
type="checkbox"
263-
value="{{ $column->getHash() }}"
263+
value="{{ $column->getSlug() }}"
264264
/>
265265
<span class="ml-2">{{ $column->getTitle() }}</span>
266266
</label>
@@ -511,7 +511,7 @@ class="px-2 {{ $loop->last ? 'mb-0' : 'mb-1' }}"
511511
wire:target="selectedColumns"
512512
wire:loading.attr="disabled"
513513
type="checkbox"
514-
value="{{ $column->getHash() }}"
514+
value="{{ $column->getSlug() }}"
515515
/>
516516
<span class="ml-2">{{ $column->getTitle() }}</span>
517517
</label>
@@ -754,7 +754,7 @@ class="px-2 {{ $loop->last ? 'mb-0' : 'mb-1' }}"
754754
wire:target="selectedColumns"
755755
wire:loading.attr="disabled"
756756
type="checkbox"
757-
value="{{ $column->getHash() }}"
757+
value="{{ $column->getSlug() }}"
758758
/>
759759
<span class="ml-2">{{ $column->getTitle() }}</span>
760760
</label>
@@ -813,4 +813,4 @@ class="d-block">
813813

814814
@if ($component->hasConfigurableAreaFor('after-toolbar'))
815815
@include($component->getConfigurableAreaFor('after-toolbar'), $component->getParametersForConfigurableArea('after-toolbar'))
816-
@endif
816+
@endif

src/DataTableComponent.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ abstract class DataTableComponent extends Component
4747
'clearFilters' => 'clearFilterEvent',
4848
];
4949

50+
/**
51+
* returns a unique id for the table, used as an alias to identify one table from another session and query string to prevent conflicts
52+
*/
53+
protected function dataTableFingerprint(): string
54+
{
55+
$className = str_split(static::class);
56+
$crc32 = sprintf('%u', crc32(serialize($className)));
57+
58+
return base_convert($crc32, 10, 36);
59+
}
60+
5061
/**
5162
* Runs on every request, immediately after the component is instantiated, but before any other lifecycle methods are called
5263
*/
@@ -55,8 +66,9 @@ public function boot(): void
5566
$this->{$this->tableName} = [
5667
'sorts' => $this->{$this->tableName}['sorts'] ?? [],
5768
'filters' => $this->{$this->tableName}['filters'] ?? [],
69+
'columns' => $this->{$this->tableName}['columns'] ?? [],
5870
];
59-
71+
6072
// Set the filter defaults based on the filter type
6173
$this->setFilterDefaults();
6274
}

src/Traits/ComponentUtilities.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function queryString(): array
5656
{
5757
if ($this->queryStringIsEnabled()) {
5858
return [
59-
$this->getTableName() => ['except' => null],
59+
$this->getTableName() => ['except' => null, 'as' => $this->getQueryStringAlias()],
6060
];
6161
}
6262

src/Traits/Configuration/ComponentConfiguration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,18 @@ public function setHideConfigurableAreasWhenReorderingDisabled(): self
355355

356356
return $this;
357357
}
358+
359+
public function setDataTableFingerprint(string $dataTableFingerprint): self
360+
{
361+
$this->dataTableFingerprint = $dataTableFingerprint;
362+
363+
return $this;
364+
}
365+
366+
public function setQueryStringAlias(string $queryStringAlias): self
367+
{
368+
$this->queryStringAlias = $queryStringAlias;
369+
370+
return $this;
371+
}
358372
}

src/Traits/Helpers/ColumnSelectHelpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function rememberColumnSelectionIsDisabled(): bool
6565
*/
6666
public function columnSelectIsEnabledForColumn($column): bool
6767
{
68-
return in_array($column instanceof Column ? $column->getHash() : $column, $this->selectedColumns, true);
68+
return in_array($column instanceof Column ? $column->getSlug() : $column, $this->selectedColumns, true);
6969
}
7070

7171
/**
@@ -82,6 +82,6 @@ protected function forgetColumnSelectSession()
8282
*/
8383
protected function getColumnSelectSessionKey()
8484
{
85-
return $this->getTableName().'-columnSelectEnabled';
85+
return $this->getDataTableFingerprint().'-columnSelectEnabled';
8686
}
8787
}

src/Traits/Helpers/ComponentHelpers.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88

99
trait ComponentHelpers
1010
{
11+
public function getDataTableFingerprint(): string
12+
{
13+
return $this->dataTableFingerprint ?? $this->dataTableFingerprint();
14+
}
15+
16+
public function getQueryStringAlias(): string
17+
{
18+
return $this->queryStringAlias ?? $this->getTableName();
19+
}
20+
1121
/**
1222
* @param Builder
1323
*/

src/Traits/WithColumnSelect.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,27 @@ public function setupColumnSelect(): void
3131
->filter(function ($column) {
3232
return $column->isVisible() && $column->isSelectable() && $column->isSelected();
3333
})
34-
->map(fn ($column) => $column->getHash())
34+
->map(fn ($column) => $column->getSlug())
3535
->values()
3636
->toArray();
3737

3838
// Set to either the default set or what is stored in the session
39-
$this->selectedColumns = session()->get($this->getColumnSelectSessionKey(), $columns);
39+
$this->selectedColumns = (isset($this->{$this->tableName}['columns']) && count($this->{$this->tableName}['columns']) > 0) ?
40+
$this->{$this->tableName}['columns'] :
41+
session()->get($this->getColumnSelectSessionKey(), $columns);
4042

4143
// Check to see if there are any excluded that are already stored in the enabled and remove them
4244
foreach ($this->getColumns() as $column) {
43-
if (! $column->isSelectable() && ! in_array($column->getHash(), $this->selectedColumns, true)) {
44-
$this->selectedColumns[] = $column->getHash();
45+
if (! $column->isSelectable() && ! in_array($column->getSlug(), $this->selectedColumns, true)) {
46+
$this->selectedColumns[] = $column->getSlug();
4547
session([$this->getColumnSelectSessionKey() => $this->selectedColumns]);
4648
}
4749
}
4850
}
4951

5052
public function updatedSelectedColumns(): void
5153
{
52-
session([$this->getColumnSelectSessionKey() => $this->selectedColumns]);
54+
$this->{$this->tableName}['columns'] = $this->selectedColumns;
55+
session([$this->getColumnSelectSessionKey() => $this->{$this->tableName}['columns']]);
5356
}
5457
}

0 commit comments

Comments
 (0)