Skip to content

Commit 63067a9

Browse files
author
=
committed
implement feature
1 parent 60d60c2 commit 63067a9

File tree

7 files changed

+27
-29
lines changed

7 files changed

+27
-29
lines changed

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

Lines changed: 3 additions & 3 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->getField() }}"
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->getField() }}"
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->getField() }}"
758758
/>
759759
<span class="ml-2">{{ $column->getTitle() }}</span>
760760
</label>

src/DataTableComponent.php

Lines changed: 11 additions & 0 deletions
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+
public 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
*/

src/Traits/ComponentUtilities.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ trait ComponentUtilities
1414

1515
public array $table = [];
1616
public $theme = null;
17+
public array $userSelectedColumns = [];
1718
protected Builder $builder;
1819
protected $model;
1920
protected $primaryKey;
@@ -56,7 +57,8 @@ public function queryString(): array
5657
{
5758
if ($this->queryStringIsEnabled()) {
5859
return [
59-
$this->getTableName() => ['except' => null],
60+
$this->getTableName() => ['except' => null, 'as' => $this->dataTableFingerprint()],
61+
'userSelectedColumns' => ['except' => null, 'as' => $this->dataTableFingerprint() . '-c'],
6062
];
6163
}
6264

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->getField() : $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->dataTableFingerprint().'-columnSelectEnabled';
8686
}
8787
}

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->getField())
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 = count($this->userSelectedColumns) > 0 ?
40+
$this->userSelectedColumns :
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->getField(), $this->selectedColumns, true)) {
46+
$this->selectedColumns[] = $column->getField();
4547
session([$this->getColumnSelectSessionKey() => $this->selectedColumns]);
4648
}
4749
}
4850
}
4951

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

src/Views/Traits/Helpers/ColumnHelpers.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@ public function getComponent(): ?DataTableComponent
2020
return $this->component;
2121
}
2222

23-
/**
24-
* @param DataTableComponent $component
25-
*
26-
* @return $this
27-
*/
28-
public function getHash(): string
29-
{
30-
return $this->hash;
31-
}
32-
3323
/**
3424
* @return bool
3525
*/

tests/Views/Traits/Helpers/ColumnHelpersTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,6 @@ public function can_get_column_formatter(): void
320320
$this->assertInstanceOf(Closure::class, $column->getFormatCallback());
321321
}
322322

323-
/** @test */
324-
public function can_get_column_hash(): void
325-
{
326-
$column = Column::make('Name');
327-
328-
$this->assertSame($column->getHash(), md5('name'));
329-
}
330-
331323
/** @test */
332324
public function can_check_if_column_has_secondary_header(): void
333325
{

0 commit comments

Comments
 (0)