Skip to content

Commit 1a10a61

Browse files
committed
Fix set total & filtered records count
Fix #1453 #1454 #2050 #2609
1 parent d13de45 commit 1a10a61

File tree

4 files changed

+147
-148
lines changed

4 files changed

+147
-148
lines changed

src/CollectionDataTable.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static function create($source)
9191
*/
9292
public function count(): int
9393
{
94-
return ($this->collection->count() > $this->totalRecords) ? $this->totalRecords : $this->collection->count();
94+
return $this->collection->count();
9595
}
9696

9797
/**
@@ -112,8 +112,6 @@ public function columnSearch(): void
112112
continue;
113113
}
114114

115-
$this->isFilterApplied = true;
116-
117115
$regex = $this->request->isRegex($i);
118116
$keyword = $this->request->columnKeyword($i);
119117

@@ -187,16 +185,6 @@ public function make($mDataSupport = true): JsonResponse
187185
}
188186
}
189187

190-
/**
191-
* Count total items.
192-
*
193-
* @return int
194-
*/
195-
public function totalCount(): int
196-
{
197-
return $this->totalRecords ?: $this->collection->count();
198-
}
199-
200188
/**
201189
* Get results.
202190
*
@@ -254,8 +242,6 @@ protected function globalSearch(string $keyword): void
254242
$keyword = $this->config->isCaseInsensitive() ? Str::lower($keyword) : $keyword;
255243

256244
$this->collection = $this->collection->filter(function ($row) use ($keyword) {
257-
$this->isFilterApplied = true;
258-
259245
$data = $this->serialize($row);
260246
foreach ($this->request->searchableColumnIndex() as $index) {
261247
$column = $this->getColumnName($index);

src/DataTableAbstract.php

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,23 @@ abstract class DataTableAbstract implements DataTable
7272
/**
7373
* Total records.
7474
*
75-
* @var int
75+
* @var int|null
7676
*/
77-
protected int $totalRecords = 0;
77+
protected ?int $totalRecords = null;
78+
79+
/**
80+
* Flag to skip total records count query.
81+
*
82+
* @var bool
83+
*/
84+
protected bool $skipTotalRecords = false;
7885

7986
/**
8087
* Total filtered records.
8188
*
82-
* @var int
89+
* @var int|null
8390
*/
84-
protected int $filteredRecords = 0;
91+
protected ?int $filteredRecords = null;
8592

8693
/**
8794
* Auto-filter flag.
@@ -109,17 +116,10 @@ abstract class DataTableAbstract implements DataTable
109116
'DT_RowAttr' => [],
110117
];
111118

112-
/**
113-
* [internal] Track if any filter was applied for at least one column.
114-
*
115-
* @var bool
116-
*/
117-
protected bool $isFilterApplied = false;
118-
119119
/**
120120
* Custom ordering callback.
121121
*
122-
* @var ?callable
122+
* @var callable|null
123123
*/
124124
protected $orderCallback = null;
125125

@@ -453,7 +453,7 @@ public function with($key, $value = ''): static
453453
* @param callable $value
454454
* @return $this
455455
*/
456-
public function withQuery($key, callable $value): static
456+
public function withQuery(string $key, callable $value): static
457457
{
458458
$this->appends[$key] = $value;
459459

@@ -492,7 +492,7 @@ public function blacklist(array $blacklist): static
492492
* @param string|array $whitelist
493493
* @return $this
494494
*/
495-
public function whitelist($whitelist = '*'): static
495+
public function whitelist(array|string $whitelist = '*'): static
496496
{
497497
$this->columnDef['whitelist'] = $whitelist;
498498

@@ -505,7 +505,7 @@ public function whitelist($whitelist = '*'): static
505505
* @param bool $state
506506
* @return $this
507507
*/
508-
public function smart($state = true): static
508+
public function smart(bool $state = true): static
509509
{
510510
$this->config->set('datatables.search.smart', $state);
511511

@@ -518,7 +518,7 @@ public function smart($state = true): static
518518
* @param bool $state
519519
* @return $this
520520
*/
521-
public function startsWithSearch($state = true): static
521+
public function startsWithSearch(bool $state = true): static
522522
{
523523
$this->config->set('datatables.search.starts_with', $state);
524524

@@ -531,7 +531,7 @@ public function startsWithSearch($state = true): static
531531
* @param bool $multiTerm
532532
* @return $this
533533
*/
534-
public function setMultiTerm($multiTerm = true): static
534+
public function setMultiTerm(bool $multiTerm = true): static
535535
{
536536
$this->config->set('datatables.search.multi_term', $multiTerm);
537537

@@ -544,20 +544,35 @@ public function setMultiTerm($multiTerm = true): static
544544
* @param int $total
545545
* @return $this
546546
*/
547-
public function setTotalRecords($total): static
547+
public function setTotalRecords(int $total): static
548548
{
549+
$this->skipTotalRecords();
549550
$this->totalRecords = $total;
550551

551552
return $this;
552553
}
553554

555+
/**
556+
* Skip total records and set the recordsTotal equals to recordsFiltered.
557+
* This will improve the performance by skipping the total count query.
558+
*
559+
* @return $this
560+
*/
561+
public function skipTotalRecords(): static
562+
{
563+
$this->skipTotalRecords = true;
564+
$this->totalRecords = 0;
565+
566+
return $this;
567+
}
568+
554569
/**
555570
* Set filtered records manually.
556571
*
557572
* @param int $total
558573
* @return $this
559574
*/
560-
public function setFilteredRecords($total): static
575+
public function setFilteredRecords(int $total): static
561576
{
562577
$this->filteredRecords = $total;
563578

@@ -651,7 +666,6 @@ abstract protected function defaultOrdering(): void;
651666
public function filter(callable $callback, $globalSearch = false): static
652667
{
653668
$this->autoFilter = $globalSearch;
654-
$this->isFilterApplied = true;
655669
$this->filterCallback = $callback;
656670

657671
return $this;
@@ -721,7 +735,7 @@ protected function filterRecords(): void
721735

722736
$this->columnSearch();
723737
$this->searchPanesSearch();
724-
$this->filteredRecords = $this->isFilterApplied ? $this->filteredCount() : $this->totalRecords;
738+
$this->filteredCount();
725739
}
726740

727741
/**
@@ -778,14 +792,28 @@ protected function searchPanesSearch(): void
778792
// Add support for search pane.
779793
}
780794

795+
/**
796+
* Count total items.
797+
*
798+
* @return int
799+
*/
800+
public function totalCount(): int
801+
{
802+
if ($this->skipTotalRecords) {
803+
return $this->totalRecords;
804+
}
805+
806+
return $this->totalRecords = $this->count();
807+
}
808+
781809
/**
782810
* Count filtered items.
783811
*
784812
* @return int
785813
*/
786814
protected function filteredCount(): int
787815
{
788-
return $this->filteredRecords ?: $this->count();
816+
return $this->filteredRecords ??= $this->count();
789817
}
790818

791819
/**

src/QueryDataTable.php

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ class QueryDataTable extends DataTableAbstract
4141
*/
4242
protected $limitCallback = null;
4343

44-
/**
45-
* Flag to skip total records count query.
46-
*
47-
* @var bool
48-
*/
49-
protected bool $skipTotalRecords = false;
50-
5144
/**
5245
* Flag to keep the select bindings.
5346
*
@@ -103,9 +96,7 @@ public static function canCreate($source): bool
10396
public function make($mDataSupport = true): JsonResponse
10497
{
10598
try {
106-
$this->prepareQuery();
107-
108-
$results = $this->results();
99+
$results = $this->prepareQuery()->results();
109100
$processed = $this->processResults($results, $mDataSupport);
110101
$data = $this->transform($results, $processed);
111102

@@ -117,8 +108,10 @@ public function make($mDataSupport = true): JsonResponse
117108

118109
/**
119110
* Prepare query by executing count, filter, order and paginate.
111+
*
112+
* @return $this
120113
*/
121-
protected function prepareQuery(): void
114+
protected function prepareQuery(): static
122115
{
123116
if (! $this->prepared) {
124117
$this->totalRecords = $this->totalCount();
@@ -131,22 +124,8 @@ protected function prepareQuery(): void
131124
}
132125

133126
$this->prepared = true;
134-
}
135127

136-
/**
137-
* Count total items.
138-
*
139-
* @return int
140-
*/
141-
public function totalCount(): int
142-
{
143-
if ($this->skipTotalRecords) {
144-
$this->isFilterApplied = true;
145-
146-
return 1;
147-
}
148-
149-
return $this->totalRecords ?: $this->count();
128+
return $this;
150129
}
151130

152131
/**
@@ -215,19 +194,6 @@ public function results(): Collection
215194
return $this->query->get();
216195
}
217196

218-
/**
219-
* Skip total records and set the recordsTotal equals to recordsFiltered.
220-
* This will improve the performance by skipping the total count query.
221-
*
222-
* @return $this
223-
*/
224-
public function skipTotalRecords(): static
225-
{
226-
$this->skipTotalRecords = true;
227-
228-
return $this;
229-
}
230-
231197
/**
232198
* Keep the select bindings.
233199
*
@@ -268,8 +234,6 @@ public function columnSearch(): void
268234
$keyword = $this->getColumnSearchKeyword($index);
269235
$this->compileColumnSearch($index, $column, $keyword);
270236
}
271-
272-
$this->isFilterApplied = true;
273237
}
274238
}
275239

@@ -628,26 +592,9 @@ protected function searchPanesSearch(): void
628592
} else {
629593
$this->query->whereIn($column, $values);
630594
}
631-
632-
$this->isFilterApplied = true;
633595
}
634596
}
635597

636-
/**
637-
* Count filtered items.
638-
*
639-
* @return int
640-
*/
641-
protected function filteredCount(): int
642-
{
643-
$this->filteredRecords = $this->filteredRecords ?: $this->count();
644-
if ($this->skipTotalRecords) {
645-
$this->totalRecords = $this->filteredRecords;
646-
}
647-
648-
return $this->filteredRecords;
649-
}
650-
651598
/**
652599
* Resolve callback parameter instance.
653600
*
@@ -769,8 +716,6 @@ protected function globalSearch(string $keyword): void
769716
} else {
770717
$this->compileQuerySearch($query, $column, $keyword);
771718
}
772-
773-
$this->isFilterApplied = true;
774719
});
775720
});
776721
}

0 commit comments

Comments
 (0)