Skip to content

Commit 512dd4e

Browse files
author
Herbert Maschke
committed
introduce possibility to persist search string in session
1 parent e070004 commit 512dd4e

File tree

2 files changed

+72
-17
lines changed

2 files changed

+72
-17
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ somewhere in your CSS
5252
```html
5353
...
5454

55-
<livewire:datatable model="App\User" />
55+
<livewire:datatable model="App\User" name="all-my-users" />
5656

5757
...
5858
```
@@ -62,11 +62,15 @@ somewhere in your CSS
6262
```html
6363
<livewire:datatable
6464
model="App\User"
65+
name="users"
6566
include="id, name, dob, created_at"
6667
dates="dob"
6768
/>
6869
```
6970

71+
- *Attention*: Please not that having multiple datatables on the same page _or_ more than one datatable of the same type on different pages needs to have a unique `name` attribute assigned to each one so they do not conflict with each other
72+
as in the example above.
73+
7074
### Props
7175
| Property | Arguments | Result | Example |
7276
|----|----|----|----|
@@ -100,7 +104,6 @@ somewhere in your CSS
100104
101105
### Declare a public method ```columns``` that returns an array containing one or more ```Mediconesystems\LivewireDatatables\Column```
102106

103-
104107
## Columns
105108
Columns can be built using any of the static methods below, and then their attributes assigned using fluent method chains.
106109
There are additional specific types of Column; ```NumberColumn```, ```DateColumn```, ```TimeColumn```, using the correct one for your datatype will enable type-specific formatting and filtering:

src/Http/Livewire/LivewireDatatable.php

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class LivewireDatatable extends Component
6161
public $name;
6262
public $columnGroups = [];
6363
public $userFilter;
64-
public $persistComplexQuery;
64+
public $persistSearch = true;
65+
public $persistComplexQuery = true;
6566
public $persistHiddenColumns = true;
6667
public $persistSort = true;
6768
public $persistPerPage = true;
@@ -127,7 +128,18 @@ public function applyToTable($options)
127128
}
128129
}
129130

130-
foreach (['perPage', 'search', 'activeSelectFilters', 'activeDateFilters', 'activeTimeFilters', 'activeBooleanFilters', 'activeTextFilters', 'activeNumberFilters', 'hide', 'selected'] as $property) {
131+
foreach ([
132+
'perPage',
133+
'search',
134+
'activeSelectFilters',
135+
'activeDateFilters',
136+
'activeTimeFilters',
137+
'activeBooleanFilters',
138+
'activeTextFilters',
139+
'activeNumberFilters',
140+
'hide',
141+
'selected',
142+
] as $property) {
131143
if (isset($options[$property])) {
132144
$this->$property = $options[$property];
133145
}
@@ -175,21 +187,47 @@ public function mount(
175187
$afterTableSlot = false,
176188
$params = []
177189
) {
178-
foreach (['model', 'include', 'exclude', 'hide', 'dates', 'times', 'searchable', 'sort', 'hideHeader', 'hidePagination', 'exportable', 'hideable', 'beforeTableSlot', 'afterTableSlot'] as $property) {
190+
foreach ([
191+
'model',
192+
'include',
193+
'exclude',
194+
'hide',
195+
'dates',
196+
'times',
197+
'searchable',
198+
'sort',
199+
'hideHeader',
200+
'hidePagination',
201+
'exportable',
202+
'hideable',
203+
'beforeTableSlot',
204+
'afterTableSlot',
205+
] as $property) {
179206
$this->$property = $this->$property ?? $$property;
180207
}
181208

182209
$this->params = $params;
183210

184211
$this->columns = $this->getViewColumns();
185212

213+
$this->initialiseSearch();
186214
$this->initialiseSort();
187215
$this->initialiseHiddenColumns();
188216
$this->initialiseFilters();
189217
$this->initialisePerPage();
190218
$this->initialiseColumnGroups();
191219
}
192220

221+
// save settings
222+
public function dehydrate()
223+
{
224+
if ($this->persistSearch) {
225+
session()->put($this->sessionStorageKey() . '_search', $this->search);
226+
}
227+
228+
return parent::dehydrate(); // @phpstan-ignore-line
229+
}
230+
193231
public function columns()
194232
{
195233
return $this->modelInstance;
@@ -465,7 +503,7 @@ public function getFreshColumnsProperty()
465503

466504
public function sessionStorageKey()
467505
{
468-
return Str::snake(Str::afterLast(get_called_class(), '\\'));
506+
return Str::snake(Str::afterLast(get_called_class(), '\\')) . $this->name;
469507
}
470508

471509
public function getSessionStoredSort()
@@ -474,8 +512,8 @@ public function getSessionStoredSort()
474512
return;
475513
}
476514

477-
$this->sort = session()->get($this->sessionStorageKey() . $this->name . '_sort', $this->sort);
478-
$this->direction = session()->get($this->sessionStorageKey() . $this->name . '_direction', $this->direction);
515+
$this->sort = session()->get($this->sessionStorageKey() . '_sort', $this->sort);
516+
$this->direction = session()->get($this->sessionStorageKey() . '_direction', $this->direction);
479517
}
480518

481519
public function getSessionStoredPerPage()
@@ -493,7 +531,10 @@ public function setSessionStoredSort()
493531
return;
494532
}
495533

496-
session()->put([$this->sessionStorageKey() . $this->name . '_sort' => $this->sort, $this->sessionStorageKey() . $this->name . '_direction' => $this->direction]);
534+
session()->put([
535+
$this->sessionStorageKey() . '_sort' => $this->sort,
536+
$this->sessionStorageKey() . '_direction' => $this->direction,
537+
]);
497538
}
498539

499540
public function setSessionStoredFilters()
@@ -503,7 +544,7 @@ public function setSessionStoredFilters()
503544
}
504545

505546
session()->put([
506-
$this->sessionStorageKey() . $this->name . '_filter' => [
547+
$this->sessionStorageKey() . '_filter' => [
507548
'text' => $this->activeTextFilters,
508549
'boolean' => $this->activeBooleanFilters,
509550
'select' => $this->activeSelectFilters,
@@ -514,6 +555,15 @@ public function setSessionStoredFilters()
514555
]);
515556
}
516557

558+
public function initialiseSearch()
559+
{
560+
if (! $this->persistSearch) {
561+
return;
562+
}
563+
564+
$this->search = session()->get($this->sessionStorageKey() . '_search', $this->search);
565+
}
566+
517567
public function initialiseSort()
518568
{
519569
$this->sort = $this->defaultSort()
@@ -532,9 +582,9 @@ public function initialiseHiddenColumns()
532582
return;
533583
}
534584

535-
if (session()->has($this->sessionStorageKey() . $this->name . '_hidden_columns')) {
585+
if (session()->has($this->sessionStorageKey() . '_hidden_columns')) {
536586
$this->columns = collect($this->columns)->map(function ($column, $index) {
537-
$column['hidden'] = in_array($index, session()->get($this->sessionStorageKey() . $this->name . '_hidden_columns'));
587+
$column['hidden'] = in_array($index, session()->get($this->sessionStorageKey() . '_hidden_columns'));
538588

539589
return $column;
540590
})->toArray();
@@ -565,7 +615,7 @@ public function initialiseFilters()
565615
return;
566616
}
567617

568-
$filters = session()->get($this->sessionStorageKey() . $this->name . '_filter');
618+
$filters = session()->get($this->sessionStorageKey() . '_filter');
569619

570620
$this->activeBooleanFilters = $filters['boolean'] ?? [];
571621
$this->activeSelectFilters = $filters['select'] ?? [];
@@ -651,8 +701,10 @@ public function sort($index, $direction = null)
651701
}
652702
$this->page = 1;
653703

654-
$key = Str::snake(Str::afterLast(get_called_class(), '\\'));
655-
session()->put([$key . $this->name . '_sort' => $this->sort, $key . $this->name . '_direction' => $this->direction]);
704+
session()->put([
705+
$this->sessionStorageKey() . '_sort' => $this->sort,
706+
$this->sessionStorageKey() . '_direction' => $this->direction,
707+
]);
656708
}
657709

658710
public function toggle($index)
@@ -670,7 +722,7 @@ public function toggle($index)
670722
if ($this->persistHiddenColumns) {
671723
$hidden = collect($this->columns)->filter->hidden->keys()->toArray();
672724

673-
session()->put([$this->sessionStorageKey() . $this->name . '_hidden_columns' => $hidden]);
725+
session()->put([$this->sessionStorageKey() . '_hidden_columns' => $hidden]);
674726
}
675727
}
676728

@@ -1472,7 +1524,7 @@ public function render()
14721524
$this->emit('refreshDynamic');
14731525

14741526
if ($this->persistPerPage) {
1475-
session()->put([$this->sessionStorageKey() . $this->name . '_perpage' => $this->perPage]);
1527+
session()->put([$this->sessionStorageKey() . '_perpage' => $this->perPage]);
14761528
}
14771529

14781530
return view('datatables::datatable')->layoutData(['title' => $this->title]);

0 commit comments

Comments
 (0)