Skip to content

Commit ddb91bb

Browse files
thyseusHerbert Maschke
andauthored
introduce ability to persist filters, perpage and hidden columns in session (MedicOneSystems#274)
* introduce ability to persist filters in session * introduce ability to persist perpage in session * introduce ability to persist hidden columns in session * use UK, not US spelling (s/initialize/initialise) Co-authored-by: Herbert Maschke <thyseus@pm.me>
1 parent b0ff8a7 commit ddb91bb

File tree

1 file changed

+116
-6
lines changed

1 file changed

+116
-6
lines changed

src/Http/Livewire/LivewireDatatable.php

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ class LivewireDatatable extends Component
5656
public $afterTableSlot;
5757
public $complex;
5858
public $complexQuery;
59-
public $persistComplexQuery;
6059
public $title;
6160
public $name;
6261
public $userFilter;
62+
public $persistComplexQuery;
63+
public $persistHiddenColumns = true;
6364
public $persistSort = true;
65+
public $persistPerPage = true;
66+
public $persistFilters = true;
6467

6568
protected $query;
6669
protected $listeners = ['refreshLivewireDatatable', 'complexQuery', 'saveQuery', 'deleteQuery', 'applyToTable', 'resetTable'];
@@ -169,8 +172,9 @@ public function mount(
169172
$this->columns = $this->getViewColumns();
170173

171174
$this->initialiseSort();
172-
173-
$this->perPage = $perPage ?? $this->perPage ?? config('livewire-datatables.default_per_page', 10);
175+
$this->initialiseHiddenColumns();
176+
$this->initialiseFilters();
177+
$this->initialisePerPage();
174178
}
175179

176180
public function columns()
@@ -446,15 +450,50 @@ public function sessionStorageKey()
446450

447451
public function getSessionStoredSort()
448452
{
453+
if (! $this->persistSort) {
454+
return;
455+
}
456+
449457
$this->sort = session()->get($this->sessionStorageKey() . $this->name . '_sort', $this->sort);
450458
$this->direction = session()->get($this->sessionStorageKey() . $this->name . '_direction', $this->direction);
451459
}
452460

461+
public function getSessionStoredPerPage()
462+
{
463+
if (! $this->persistPerPage) {
464+
return;
465+
}
466+
467+
$this->perPage = session()->get($this->sessionStorageKey() . $this->name . '_perpage', $this->perPage);
468+
}
469+
453470
public function setSessionStoredSort()
454471
{
472+
if (! $this->persistSort) {
473+
return;
474+
}
475+
455476
session()->put([$this->sessionStorageKey() . $this->name . '_sort' => $this->sort, $this->sessionStorageKey() . $this->name . '_direction' => $this->direction]);
456477
}
457478

479+
public function setSessionStoredFilters()
480+
{
481+
if (! $this->persistFilters) {
482+
return;
483+
}
484+
485+
session()->put([
486+
$this->sessionStorageKey() . $this->name . '_filter' => [
487+
'text' => $this->activeTextFilters,
488+
'boolean' => $this->activeBooleanFilters,
489+
'select' => $this->activeSelectFilters,
490+
'date' => $this->activeDateFilters,
491+
'time' => $this->activeTimeFilters,
492+
'number' => $this->activeNumberFilters,
493+
],
494+
]);
495+
}
496+
458497
public function sort($index)
459498
{
460499
if ($this->sort === (int) $index) {
@@ -477,13 +516,53 @@ public function initialiseSort()
477516
return $column['type'] === 'checkbox' || $column['hidden'];
478517
})->keys()->first();
479518

480-
if ($this->persistSort) {
481-
$this->getSessionStoredSort();
482-
}
519+
$this->getSessionStoredSort();
483520

484521
$this->direction = $this->defaultSort() && $this->defaultSort()['direction'] === 'asc';
485522
}
486523

524+
public function initialiseHiddenColumns()
525+
{
526+
if (! $this->persistHiddenColumns) {
527+
return;
528+
}
529+
530+
if (session()->has($this->sessionStorageKey() . $this->name . '_hidden_columns')) {
531+
foreach (session()->get($this->sessionStorageKey() . $this->name . '_hidden_columns') as $name) {
532+
foreach ($this->columns as $key => $column) {
533+
if ($column['name'] === $name) {
534+
$this->columns[$key]['hidden'] = 1;
535+
}
536+
}
537+
}
538+
}
539+
}
540+
541+
public function initialisePerPage()
542+
{
543+
$this->getSessionStoredPerPage();
544+
545+
if (! $this->perPage) {
546+
$this->perPage = $this->perPage ?? config('livewire-datatables.default_per_page', 10);
547+
}
548+
}
549+
550+
public function initialiseFilters()
551+
{
552+
if (! $this->persistFilters) {
553+
return;
554+
}
555+
556+
$filters = session()->get($this->sessionStorageKey() . $this->name . '_filter');
557+
558+
$this->activeBooleanFilters = $filters['boolean'] ?? [];
559+
$this->activeSelect = $filters['select'] ?? [];
560+
$this->activeTextFilters = $filters['text'] ?? [];
561+
$this->activeDateFilters = $filters['date'] ?? [];
562+
$this->activeTimeFilters = $filters['time'] ?? [];
563+
$this->activeNumberFilters = $filters['number'] ?? [];
564+
}
565+
487566
public function defaultSort()
488567
{
489568
$columnIndex = collect($this->freshColumns)->search(function ($column) {
@@ -575,18 +654,32 @@ public function toggle($index)
575654
}
576655

577656
$this->columns[$index]['hidden'] = ! $this->columns[$index]['hidden'];
657+
658+
if ($this->persistHiddenColumns) {
659+
$hidden = [];
660+
661+
foreach ($this->columns as $column) {
662+
if ($column['hidden']) {
663+
$hidden[] = $column['name'];
664+
}
665+
}
666+
667+
session()->put([$this->sessionStorageKey() . $this->name . '_hidden_columns' => $hidden]);
668+
}
578669
}
579670

580671
public function doBooleanFilter($index, $value)
581672
{
582673
$this->activeBooleanFilters[$index] = $value;
583674
$this->page = 1;
675+
$this->setSessionStoredFilters();
584676
}
585677

586678
public function doSelectFilter($index, $value)
587679
{
588680
$this->activeSelectFilters[$index][] = $value;
589681
$this->page = 1;
682+
$this->setSessionStoredFilters();
590683
}
591684

592685
public function doTextFilter($index, $value)
@@ -596,44 +689,51 @@ public function doTextFilter($index, $value)
596689
}
597690

598691
$this->page = 1;
692+
$this->setSessionStoredFilters();
599693
}
600694

601695
public function doDateFilterStart($index, $start)
602696
{
603697
$this->activeDateFilters[$index]['start'] = $start;
604698
$this->page = 1;
699+
$this->setSessionStoredFilters();
605700
}
606701

607702
public function doDateFilterEnd($index, $end)
608703
{
609704
$this->activeDateFilters[$index]['end'] = $end;
610705
$this->page = 1;
706+
$this->setSessionStoredFilters();
611707
}
612708

613709
public function doTimeFilterStart($index, $start)
614710
{
615711
$this->activeTimeFilters[$index]['start'] = $start;
616712
$this->page = 1;
713+
$this->setSessionStoredFilters();
617714
}
618715

619716
public function doTimeFilterEnd($index, $end)
620717
{
621718
$this->activeTimeFilters[$index]['end'] = $end;
622719
$this->page = 1;
720+
$this->setSessionStoredFilters();
623721
}
624722

625723
public function doNumberFilterStart($index, $start)
626724
{
627725
$this->activeNumberFilters[$index]['start'] = $start ? (int) $start : null;
628726
$this->clearEmptyNumberFilter($index);
629727
$this->page = 1;
728+
$this->setSessionStoredFilters();
630729
}
631730

632731
public function doNumberFilterEnd($index, $end)
633732
{
634733
$this->activeNumberFilters[$index]['end'] = ($end !== '') ? (int) $end : null;
635734
$this->clearEmptyNumberFilter($index);
636735
$this->page = 1;
736+
$this->setSessionStoredFilters();
637737
}
638738

639739
public function clearEmptyNumberFilter($index)
@@ -642,6 +742,7 @@ public function clearEmptyNumberFilter($index)
642742
$this->removeNumberFilter($index);
643743
}
644744
$this->page = 1;
745+
$this->setSessionStoredFilters();
645746
}
646747

647748
public function removeSelectFilter($column, $key = null)
@@ -651,6 +752,7 @@ public function removeSelectFilter($column, $key = null)
651752
unset($this->activeSelectFilters[$column]);
652753
}
653754
$this->page = 1;
755+
$this->setSessionStoredFilters();
654756
}
655757

656758
public function clearAllFilters()
@@ -664,13 +766,15 @@ public function clearAllFilters()
664766
$this->complexQuery = null;
665767
$this->userFilter = null;
666768
$this->page = 1;
769+
$this->setSessionStoredFilters();
667770

668771
$this->emitTo('complex-query', 'resetQuery');
669772
}
670773

671774
public function removeBooleanFilter($column)
672775
{
673776
unset($this->activeBooleanFilters[$column]);
777+
$this->setSessionStoredFilters();
674778
}
675779

676780
public function removeTextFilter($column, $key = null)
@@ -683,11 +787,13 @@ public function removeTextFilter($column, $key = null)
683787
} else {
684788
unset($this->activeTextFilters[$column]);
685789
}
790+
$this->setSessionStoredFilters();
686791
}
687792

688793
public function removeNumberFilter($column)
689794
{
690795
unset($this->activeNumberFilters[$column]);
796+
$this->setSessionStoredFilters();
691797
}
692798

693799
public function getColumnFilterStatement($index)
@@ -1316,6 +1422,10 @@ public function render()
13161422
{
13171423
$this->emit('refreshDynamic');
13181424

1425+
if ($this->persistPerPage) {
1426+
session()->put([$this->sessionStorageKey() . $this->name . '_perpage' => $this->perPage]);
1427+
}
1428+
13191429
return view('datatables::datatable')->layoutData(['title' => $this->title]);
13201430
}
13211431

0 commit comments

Comments
 (0)