Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 1 addition & 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,7 +253,7 @@ 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();
Expand Down
24 changes: 23 additions & 1 deletion tests/Integration/QueryDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,37 @@ public function it_can_set_total_records()
$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 10,
'recordsFiltered' => 20,
'recordsFiltered' => 10,
]);
}

#[Test]
public function it_can_set_zero_total_records()
{
$crawler = $this->call('GET', '/zero-total-records');
$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 0,
'recordsFiltered' => 0,
]);
}

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

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

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

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

#[Test]
Expand Down Expand Up @@ -463,6 +481,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