Skip to content

Commit

Permalink
Additional fix & test for zero total records.
Browse files Browse the repository at this point in the history
  • Loading branch information
yajra committed May 8, 2022
1 parent 2b72d19 commit 2c70c6a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,6 @@ public function setMultiTerm(bool $multiTerm = true): static
*/
public function setTotalRecords(int $total): static
{
$this->skipTotalRecords();
$this->totalRecords = $total;

return $this;
Expand Down
8 changes: 3 additions & 5 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ protected function prepareQuery(): static
if (! $this->prepared) {
$this->totalRecords = $this->totalCount();

if ($this->totalRecords) {
$this->filterRecords();
$this->ordering();
$this->paginate();
}
$this->filterRecords();
$this->ordering();
$this->paginate();
}

$this->prepared = true;
Expand Down
19 changes: 18 additions & 1 deletion tests/Integration/QueryDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public function it_can_set_total_records()
]);
}

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

/** @test */
public function it_can_set_total_filtered_records()
{
Expand Down Expand Up @@ -83,7 +94,7 @@ public function it_can_skip_total_records_count_query()
$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 0,
'recordsFiltered' => 0,
'recordsFiltered' => 1,
]);
}

Expand Down Expand Up @@ -429,6 +440,12 @@ protected function setUp(): void
->toJson();
});

$router->get('/zero-total-records', function (DataTables $dataTable) {
return $dataTable->query(DB::table('users'))
->setTotalRecords(0)
->toJson();
});

$router->get('/set-filtered-records', function (DataTables $dataTable) {
return $dataTable->query(DB::table('users'))
->setFilteredRecords(10)
Expand Down

0 comments on commit 2c70c6a

Please sign in to comment.