Skip to content

Commit f2f5a40

Browse files
author
Herbert Maschke
committed
introduce applyPinnedRecords()
1 parent 413654c commit f2f5a40

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/Http/Livewire/LivewireDatatable.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,6 @@ public function resetTable()
179179
$this->hide = null;
180180
$this->resetHiddenColumns();
181181
$this->selected = [];
182-
if (isset($this->pinnedRecords)) {
183-
$this->pinnedRecords = [];
184-
}
185182
}
186183

187184
/**
@@ -1187,6 +1184,7 @@ public function buildDatabaseQuery($export = false)
11871184
->addDateRangeFilter()
11881185
->addTimeRangeFilter()
11891186
->addComplexQuery()
1187+
->applyPinnedRecords()
11901188
->addSort();
11911189
}
11921190

src/Traits/CanPinRecords.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ trait CanPinRecords
2020
{
2121
public array $pinnedRecords = [];
2222

23-
public string $sessionKeyPrefix = '_pinned_records';
23+
public string $sessionKeyPostfix = '_pinned_records';
2424

2525
public function buildActions()
2626
{
@@ -45,6 +45,12 @@ public function buildActions()
4545
]);
4646
}
4747

48+
public function resetTable()
49+
{
50+
parent::resetTable();
51+
$this->pinnedRecords = [];
52+
}
53+
4854
protected function initialisePinnedRecords()
4955
{
5056
if (session()->has($this->sessionKey())) {
@@ -54,8 +60,23 @@ protected function initialisePinnedRecords()
5460
$this->selected = $this->pinnedRecords;
5561
}
5662

63+
/**
64+
* This function should be called after every filter method to ensure pinned records appear
65+
* in every possible filter combination.
66+
* Ensures to have at least _one other_ where applied to the current query build
67+
* to apply this orWhere() on top of that.
68+
*/
69+
protected function applyPinnedRecords(): self
70+
{
71+
if (isset($this->pinnedRecords) && $this->pinnedRecords && $this->query->getQuery()->wheres) {
72+
$this->query->orWhereIn('id', $this->pinnedRecords);
73+
}
74+
75+
return $this;
76+
}
77+
5778
private function sessionKey(): string
5879
{
59-
return $this->sessionStorageKey() . $this->sessionKeyPrefix;
80+
return $this->sessionStorageKey() . $this->sessionKeyPostfix;
6081
}
6182
}

0 commit comments

Comments
 (0)