Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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