Skip to content

Commit

Permalink
Merge pull request #3170 from JurianArie/option-1-add-skip-total-records
Browse files Browse the repository at this point in the history
fix: skipTotalRecords and setTotalRecords
  • Loading branch information
yajra authored Aug 26, 2024
2 parents 178a25d + 5a56e31 commit 6530b75
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
8 changes: 6 additions & 2 deletions src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ abstract class DataTableAbstract implements DataTable
*/
protected ?int $filteredRecords = null;

/**
* Flag to check if the total records count should be skipped.
*/
protected bool $skipTotalRecords = false;

/**
* Auto-filter flag.
*/
Expand Down Expand Up @@ -533,12 +538,11 @@ public function setTotalRecords(int $total): static
* This will improve the performance by skipping the total count query.
*
* @return $this
*
* @deprecated Just use setTotalRecords instead.
*/
public function skipTotalRecords(): static
{
$this->totalRecords = 0;
$this->skipTotalRecords = true;

return $this;
}
Expand Down
25 changes: 5 additions & 20 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ class QueryDataTable extends DataTableAbstract
*/
protected bool $prepared = false;

/**
* Flag to check if the total records count query has been performed.
*/
protected bool $performedTotalRecordsCount = false;

/**
* Query callback for custom pagination using limit without offset.
*
Expand Down Expand Up @@ -162,20 +157,6 @@ public function prepareQuery(): static
return $this;
}

/**
* Count total items.
*/
public function totalCount(): int
{
if ($this->totalRecords !== null) {
return $this->totalRecords;
}

$this->performedTotalRecordsCount = true;

return $this->totalRecords = $this->count();
}

/**
* Counts current query.
*/
Expand Down Expand Up @@ -272,10 +253,14 @@ protected function filterRecords(): void

// If no modification between the original query and the filtered one has been made
// the filteredRecords equals the totalRecords
if ($this->query == $initialQuery && $this->performedTotalRecordsCount) {
if (! $this->skipTotalRecords && $this->query == $initialQuery) {
$this->filteredRecords ??= $this->totalRecords;
} else {
$this->filteredCount();

if ($this->skipTotalRecords) {
$this->totalRecords = $this->filteredRecords;
}
}
}

Expand Down
39 changes: 33 additions & 6 deletions tests/Integration/QueryDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function it_can_set_total_records()
$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 10,
'recordsFiltered' => 20,
'recordsFiltered' => 10,
]);
}

Expand All @@ -37,7 +37,7 @@ public function it_can_set_zero_total_records()
$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 0,
'recordsFiltered' => 20,
'recordsFiltered' => 0,
]);
}

Expand Down Expand Up @@ -91,7 +91,27 @@ public function it_can_perform_global_search()
#[Test]
public function it_can_skip_total_records_count_query()
{
$crawler = $this->call('GET', '/query/simple', [
DB::enableQueryLog();

$crawler = $this->call('GET', '/skip-total-records');
$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 20,
]);

DB::disableQueryLog();
$queryLog = DB::getQueryLog();

$this->assertCount(2, $queryLog);
}

#[Test]
public function it_can_skip_total_records_count_query_with_filter_applied()
{
DB::enableQueryLog();

$crawler = $this->call('GET', '/skip-total-records', [
'columns' => [
['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'],
['data' => 'email', 'name' => 'email', 'searchable' => 'true', 'orderable' => 'true'],
Expand All @@ -101,9 +121,14 @@ public function it_can_skip_total_records_count_query()

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 0,
'recordsTotal' => 1,
'recordsFiltered' => 1,
]);

DB::disableQueryLog();
$queryLog = DB::getQueryLog();

$this->assertCount(2, $queryLog);
}

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

$router->get('/query/simple', fn (DataTables $dataTable) => $dataTable->query(DB::table('users'))->skipTotalRecords()->toJson());

$router->get('/query/addColumn', fn (DataTables $dataTable) => $dataTable->query(DB::table('users'))
->addColumn('foo', 'bar')
->toJson());
Expand Down Expand Up @@ -463,6 +486,10 @@ protected function setUp(): void
->setTotalRecords(0)
->toJson());

$router->get('/skip-total-records', fn (DataTables $dataTable) => $dataTable->query(DB::table('users'))
->skipTotalRecords()
->toJson());

$router->get('/set-filtered-records', fn (DataTables $dataTable) => $dataTable->query(DB::table('users'))
->setFilteredRecords(10)
->toJson());
Expand Down

0 comments on commit 6530b75

Please sign in to comment.