Skip to content

Commit

Permalink
Set total records equal to filtered records when skipping total records
Browse files Browse the repository at this point in the history
  • Loading branch information
JurianArie committed Aug 24, 2024
1 parent f3cb497 commit 5a56e31
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ protected function filterRecords(): void
$this->filteredRecords ??= $this->totalRecords;
} else {
$this->filteredCount();

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

Expand Down
49 changes: 27 additions & 22 deletions tests/Integration/QueryDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,6 @@ public function it_can_set_zero_total_records()
]);
}

#[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]
public function it_can_set_total_filtered_records()
{
Expand Down Expand Up @@ -109,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 @@ -119,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 @@ -411,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

0 comments on commit 5a56e31

Please sign in to comment.