Skip to content

Commit 40f3a9c

Browse files
committed
Apply formats only to the current page if not applied on all
1 parent 966a1bf commit 40f3a9c

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

resources/views/livewire/table.blade.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class="table-wrapper"
2626
>
2727

2828
<div class="d-flex justify-content-end alig-items-center gap-3 m-3">
29-
<x-model-browser::filter :$filter :$viewAttributes />
29+
@if (!empty ($this->filterAttributes))
30+
<x-model-browser::filter :$filter :$viewAttributes />
31+
@endif
3032
<div class="mt-3">
3133
<x-model-browser::fullscreen-button />
3234
</div>

src/Components/BaseModelBrowser.php

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class BaseModelBrowser extends Component
2121

2222
public const PER_PAGE_MIN = 3;
2323
public const PER_PAGE_MAX = 150;
24-
public const PER_PAGE_DEFAULT = 50;
24+
public const PER_PAGE_DEFAULT = 20;
2525

2626
#[Locked]
2727
public string $model;
@@ -177,33 +177,37 @@ protected function getData(bool $paginate = true, bool $highlightMatches = true,
177177
}
178178
}
179179

180-
if ($applyFormats) {
180+
$applyFormatsOnAll = ! empty($this->filterAttributes) || $this->enableSort;
181+
182+
if ($applyFormats && $applyFormatsOnAll) {
181183
$data = $this->format($data);
182184
}
183185

184-
if ($this->filter) {
186+
if (! empty($this->filterAttributes) && $this->filter) {
185187
$data = $this->applyFilter($data);
186188
}
187189

188-
// Multi-column sort
189-
$sort = $this->sort;
190-
foreach ($this->defaultSort as $attribute => $direction) {
191-
if (! isset($sort[$attribute])) {
192-
$sort[$attribute] = $direction;
190+
if ($this->enableSort) {
191+
// Multi-column sort
192+
$sort = $this->sort;
193+
foreach ($this->defaultSort as $attribute => $direction) {
194+
if (! isset($sort[$attribute])) {
195+
$sort[$attribute] = $direction;
196+
}
193197
}
194-
}
195-
if (! empty($sort)) {
196-
$sortByArg = [];
197-
foreach ($sort as $attribute => $direction) {
198-
if (is_callable($this->sortComparators[$attribute][$direction] ?? null)) {
199-
$sortByArg[] = Closure::fromCallable($this->sortComparators[$attribute][$direction]);
200-
} else {
201-
$sortByArg[] = fn ($a, $b) => $direction === 'desc'
202-
? str($this->itemValueStripped($b, $attribute))->ascii() <=> str($this->itemValueStripped($a, $attribute))->ascii()
203-
: str($this->itemValueStripped($a, $attribute))->ascii() <=> str($this->itemValueStripped($b, $attribute))->ascii();
198+
if (! empty($sort)) {
199+
$sortByArg = [];
200+
foreach ($sort as $attribute => $direction) {
201+
if (is_callable($this->sortComparators[$attribute][$direction] ?? null)) {
202+
$sortByArg[] = Closure::fromCallable($this->sortComparators[$attribute][$direction]);
203+
} else {
204+
$sortByArg[] = fn ($a, $b) => $direction === 'desc'
205+
? str($this->itemValueStripped($b, $attribute))->ascii() <=> str($this->itemValueStripped($a, $attribute))->ascii()
206+
: str($this->itemValueStripped($a, $attribute))->ascii() <=> str($this->itemValueStripped($b, $attribute))->ascii();
207+
}
204208
}
209+
$data = $data->sortBy($sortByArg);
205210
}
206-
$data = $data->sortBy($sortByArg);
207211
}
208212

209213
// Paginate manually if required
@@ -222,6 +226,17 @@ protected function getData(bool $paginate = true, bool $highlightMatches = true,
222226
}
223227
}
224228

229+
if ($applyFormats && ! $applyFormatsOnAll) {
230+
// Apply formats only to the current page if not applied on all
231+
if ($paginate) {
232+
$data->setCollection(
233+
$this->format($data->getCollection())
234+
);
235+
} else {
236+
$data = $this->format($data);
237+
}
238+
}
239+
225240
if ($highlightMatches) {
226241
$highlightedColumns = $this->filterColumn == 'all' ? $this->filterAttributes : [$this->filterColumn];
227242
if ($paginate) {

0 commit comments

Comments
 (0)