Skip to content

Commit 6530b75

Browse files
authored
Merge pull request #3170 from JurianArie/option-1-add-skip-total-records
fix: skipTotalRecords and setTotalRecords
2 parents 178a25d + 5a56e31 commit 6530b75

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

src/DataTableAbstract.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ abstract class DataTableAbstract implements DataTable
6868
*/
6969
protected ?int $filteredRecords = null;
7070

71+
/**
72+
* Flag to check if the total records count should be skipped.
73+
*/
74+
protected bool $skipTotalRecords = false;
75+
7176
/**
7277
* Auto-filter flag.
7378
*/
@@ -533,12 +538,11 @@ public function setTotalRecords(int $total): static
533538
* This will improve the performance by skipping the total count query.
534539
*
535540
* @return $this
536-
*
537-
* @deprecated Just use setTotalRecords instead.
538541
*/
539542
public function skipTotalRecords(): static
540543
{
541544
$this->totalRecords = 0;
545+
$this->skipTotalRecords = true;
542546

543547
return $this;
544548
}

src/QueryDataTable.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ class QueryDataTable extends DataTableAbstract
2525
*/
2626
protected bool $prepared = false;
2727

28-
/**
29-
* Flag to check if the total records count query has been performed.
30-
*/
31-
protected bool $performedTotalRecordsCount = false;
32-
3328
/**
3429
* Query callback for custom pagination using limit without offset.
3530
*
@@ -162,20 +157,6 @@ public function prepareQuery(): static
162157
return $this;
163158
}
164159

165-
/**
166-
* Count total items.
167-
*/
168-
public function totalCount(): int
169-
{
170-
if ($this->totalRecords !== null) {
171-
return $this->totalRecords;
172-
}
173-
174-
$this->performedTotalRecordsCount = true;
175-
176-
return $this->totalRecords = $this->count();
177-
}
178-
179160
/**
180161
* Counts current query.
181162
*/
@@ -272,10 +253,14 @@ protected function filterRecords(): void
272253

273254
// If no modification between the original query and the filtered one has been made
274255
// the filteredRecords equals the totalRecords
275-
if ($this->query == $initialQuery && $this->performedTotalRecordsCount) {
256+
if (! $this->skipTotalRecords && $this->query == $initialQuery) {
276257
$this->filteredRecords ??= $this->totalRecords;
277258
} else {
278259
$this->filteredCount();
260+
261+
if ($this->skipTotalRecords) {
262+
$this->totalRecords = $this->filteredRecords;
263+
}
279264
}
280265
}
281266

tests/Integration/QueryDataTableTest.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function it_can_set_total_records()
2626
$crawler->assertJson([
2727
'draw' => 0,
2828
'recordsTotal' => 10,
29-
'recordsFiltered' => 20,
29+
'recordsFiltered' => 10,
3030
]);
3131
}
3232

@@ -37,7 +37,7 @@ public function it_can_set_zero_total_records()
3737
$crawler->assertJson([
3838
'draw' => 0,
3939
'recordsTotal' => 0,
40-
'recordsFiltered' => 20,
40+
'recordsFiltered' => 0,
4141
]);
4242
}
4343

@@ -91,7 +91,27 @@ public function it_can_perform_global_search()
9191
#[Test]
9292
public function it_can_skip_total_records_count_query()
9393
{
94-
$crawler = $this->call('GET', '/query/simple', [
94+
DB::enableQueryLog();
95+
96+
$crawler = $this->call('GET', '/skip-total-records');
97+
$crawler->assertJson([
98+
'draw' => 0,
99+
'recordsTotal' => 20,
100+
'recordsFiltered' => 20,
101+
]);
102+
103+
DB::disableQueryLog();
104+
$queryLog = DB::getQueryLog();
105+
106+
$this->assertCount(2, $queryLog);
107+
}
108+
109+
#[Test]
110+
public function it_can_skip_total_records_count_query_with_filter_applied()
111+
{
112+
DB::enableQueryLog();
113+
114+
$crawler = $this->call('GET', '/skip-total-records', [
95115
'columns' => [
96116
['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'],
97117
['data' => 'email', 'name' => 'email', 'searchable' => 'true', 'orderable' => 'true'],
@@ -101,9 +121,14 @@ public function it_can_skip_total_records_count_query()
101121

102122
$crawler->assertJson([
103123
'draw' => 0,
104-
'recordsTotal' => 0,
124+
'recordsTotal' => 1,
105125
'recordsFiltered' => 1,
106126
]);
127+
128+
DB::disableQueryLog();
129+
$queryLog = DB::getQueryLog();
130+
131+
$this->assertCount(2, $queryLog);
107132
}
108133

109134
#[Test]
@@ -393,8 +418,6 @@ protected function setUp(): void
393418
->formatColumn('created_at', new DateFormatter('Y-m-d'))
394419
->toJson());
395420

396-
$router->get('/query/simple', fn (DataTables $dataTable) => $dataTable->query(DB::table('users'))->skipTotalRecords()->toJson());
397-
398421
$router->get('/query/addColumn', fn (DataTables $dataTable) => $dataTable->query(DB::table('users'))
399422
->addColumn('foo', 'bar')
400423
->toJson());
@@ -463,6 +486,10 @@ protected function setUp(): void
463486
->setTotalRecords(0)
464487
->toJson());
465488

489+
$router->get('/skip-total-records', fn (DataTables $dataTable) => $dataTable->query(DB::table('users'))
490+
->skipTotalRecords()
491+
->toJson());
492+
466493
$router->get('/set-filtered-records', fn (DataTables $dataTable) => $dataTable->query(DB::table('users'))
467494
->setFilteredRecords(10)
468495
->toJson());

0 commit comments

Comments
 (0)