diff --git a/src/DataTableAbstract.php b/src/DataTableAbstract.php index 4de79c2b..51051fa5 100644 --- a/src/DataTableAbstract.php +++ b/src/DataTableAbstract.php @@ -539,7 +539,6 @@ public function setMultiTerm(bool $multiTerm = true): static */ public function setTotalRecords(int $total): static { - $this->skipTotalRecords(); $this->totalRecords = $total; return $this; diff --git a/src/QueryDataTable.php b/src/QueryDataTable.php index e52d4a1d..7b832c43 100644 --- a/src/QueryDataTable.php +++ b/src/QueryDataTable.php @@ -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; diff --git a/tests/Integration/QueryDataTableTest.php b/tests/Integration/QueryDataTableTest.php index f9fae655..b14dd2a9 100644 --- a/tests/Integration/QueryDataTableTest.php +++ b/tests/Integration/QueryDataTableTest.php @@ -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() { @@ -83,7 +94,7 @@ public function it_can_skip_total_records_count_query() $crawler->assertJson([ 'draw' => 0, 'recordsTotal' => 0, - 'recordsFiltered' => 0, + 'recordsFiltered' => 1, ]); } @@ -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)