diff --git a/src/QueryDataTable.php b/src/QueryDataTable.php index 7856a03f..946f1ccf 100644 --- a/src/QueryDataTable.php +++ b/src/QueryDataTable.php @@ -257,6 +257,10 @@ protected function filterRecords(): void $this->filteredRecords ??= $this->totalRecords; } else { $this->filteredCount(); + + if ($this->skipTotalRecords) { + $this->totalRecords = $this->filteredRecords; + } } } diff --git a/tests/Integration/QueryDataTableTest.php b/tests/Integration/QueryDataTableTest.php index b5afba81..24aeb8f2 100644 --- a/tests/Integration/QueryDataTableTest.php +++ b/tests/Integration/QueryDataTableTest.php @@ -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() { @@ -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'], @@ -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] @@ -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());