Skip to content

Commit 95f25bf

Browse files
thyseusHerbert Maschke
andauthored
introduce possibility to persist search string in session (MedicOneSystems#360)
Co-authored-by: Herbert Maschke <thyseus@pm.me>
1 parent f2e9374 commit 95f25bf

File tree

2 files changed

+71
-17
lines changed

2 files changed

+71
-17
lines changed

README.md

Lines changed: 4 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,14 @@ 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 note 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 as in the example above.
72+
7073
### Props
7174
| Property | Arguments | Result | Example |
7275
|----|----|----|----|
@@ -100,7 +103,6 @@ somewhere in your CSS
100103
101104
### Declare a public method ```columns``` that returns an array containing one or more ```Mediconesystems\LivewireDatatables\Column```
102105

103-
104106
## Columns
105107
Columns can be built using any of the static methods below, and then their attributes assigned using fluent method chains.
106108
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;
@@ -128,7 +129,18 @@ public function applyToTable($options)
128129
}
129130
}
130131

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

183210
$this->params = $params;
184211

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

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

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

467505
public function sessionStorageKey()
468506
{
469-
return Str::snake(Str::afterLast(get_called_class(), '\\'));
507+
return Str::snake(Str::afterLast(get_called_class(), '\\')) . $this->name;
470508
}
471509

472510
public function getSessionStoredSort()
@@ -475,8 +513,8 @@ public function getSessionStoredSort()
475513
return;
476514
}
477515

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

482520
public function getSessionStoredPerPage()
@@ -494,7 +532,10 @@ public function setSessionStoredSort()
494532
return;
495533
}
496534

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

500541
public function setSessionStoredFilters()
@@ -504,7 +545,7 @@ public function setSessionStoredFilters()
504545
}
505546

506547
session()->put([
507-
$this->sessionStorageKey() . $this->name . '_filter' => [
548+
$this->sessionStorageKey() . '_filter' => [
508549
'text' => $this->activeTextFilters,
509550
'boolean' => $this->activeBooleanFilters,
510551
'select' => $this->activeSelectFilters,
@@ -515,6 +556,15 @@ public function setSessionStoredFilters()
515556
]);
516557
}
517558

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

536-
if (session()->has($this->sessionStorageKey() . $this->name . '_hidden_columns')) {
586+
if (session()->has($this->sessionStorageKey() . '_hidden_columns')) {
537587
$this->columns = collect($this->columns)->map(function ($column, $index) {
538-
$column['hidden'] = in_array($index, session()->get($this->sessionStorageKey() . $this->name . '_hidden_columns'));
588+
$column['hidden'] = in_array($index, session()->get($this->sessionStorageKey() . '_hidden_columns'));
539589

540590
return $column;
541591
})->toArray();
@@ -566,7 +616,7 @@ public function initialiseFilters()
566616
return;
567617
}
568618

569-
$filters = session()->get($this->sessionStorageKey() . $this->name . '_filter');
619+
$filters = session()->get($this->sessionStorageKey() . '_filter');
570620

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

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

659711
public function toggle($index)
@@ -671,7 +723,7 @@ public function toggle($index)
671723
if ($this->persistHiddenColumns) {
672724
$hidden = collect($this->columns)->filter->hidden->keys()->toArray();
673725

674-
session()->put([$this->sessionStorageKey() . $this->name . '_hidden_columns' => $hidden]);
726+
session()->put([$this->sessionStorageKey() . '_hidden_columns' => $hidden]);
675727
}
676728
}
677729

@@ -1475,7 +1527,7 @@ public function render()
14751527
$this->emit('refreshDynamic');
14761528

14771529
if ($this->persistPerPage) {
1478-
session()->put([$this->sessionStorageKey() . $this->name . '_perpage' => $this->perPage]);
1530+
session()->put([$this->sessionStorageKey() . '_perpage' => $this->perPage]);
14791531
}
14801532

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

0 commit comments

Comments
 (0)