File tree Expand file tree Collapse file tree 3 files changed +30
-23
lines changed
Expand file tree Collapse file tree 3 files changed +30
-23
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,11 @@ abstract class DataTableAbstract implements DataTable
6868 */
6969 protected ?int $ filteredRecords = null ;
7070
71+ /**
72+ * Flag to check if the total records count should be skipped.
73+ */
74+ protected bool $ skipTotalRecords = false ;
75+
7176 /**
7277 * Auto-filter flag.
7378 */
@@ -533,12 +538,11 @@ public function setTotalRecords(int $total): static
533538 * This will improve the performance by skipping the total count query.
534539 *
535540 * @return $this
536- *
537- * @deprecated Just use setTotalRecords instead.
538541 */
539542 public function skipTotalRecords (): static
540543 {
541544 $ this ->totalRecords = 0 ;
545+ $ this ->skipTotalRecords = true ;
542546
543547 return $ this ;
544548 }
Original file line number Diff line number Diff line change @@ -25,11 +25,6 @@ class QueryDataTable extends DataTableAbstract
2525 */
2626 protected bool $ prepared = false ;
2727
28- /**
29- * Flag to check if the total records count query has been performed.
30- */
31- protected bool $ performedTotalRecordsCount = false ;
32-
3328 /**
3429 * Query callback for custom pagination using limit without offset.
3530 *
@@ -162,20 +157,6 @@ public function prepareQuery(): static
162157 return $ this ;
163158 }
164159
165- /**
166- * Count total items.
167- */
168- public function totalCount (): int
169- {
170- if ($ this ->totalRecords !== null ) {
171- return $ this ->totalRecords ;
172- }
173-
174- $ this ->performedTotalRecordsCount = true ;
175-
176- return $ this ->totalRecords = $ this ->count ();
177- }
178-
179160 /**
180161 * Counts current query.
181162 */
@@ -272,7 +253,7 @@ protected function filterRecords(): void
272253
273254 // If no modification between the original query and the filtered one has been made
274255 // the filteredRecords equals the totalRecords
275- if ($ this ->query == $ initialQuery && $ this ->performedTotalRecordsCount ) {
256+ if (! $ this ->skipTotalRecords && $ this ->query == $ initialQuery ) {
276257 $ this ->filteredRecords ??= $ this ->totalRecords ;
277258 } else {
278259 $ this ->filteredCount ();
Original file line number Diff line number Diff line change @@ -26,19 +26,37 @@ public function it_can_set_total_records()
2626 $ crawler ->assertJson ([
2727 'draw ' => 0 ,
2828 'recordsTotal ' => 10 ,
29- 'recordsFiltered ' => 20 ,
29+ 'recordsFiltered ' => 10 ,
3030 ]);
3131 }
3232
3333 #[Test]
3434 public function it_can_set_zero_total_records ()
3535 {
3636 $ crawler = $ this ->call ('GET ' , '/zero-total-records ' );
37+ $ crawler ->assertJson ([
38+ 'draw ' => 0 ,
39+ 'recordsTotal ' => 0 ,
40+ 'recordsFiltered ' => 0 ,
41+ ]);
42+ }
43+
44+ #[Test]
45+ public function it_can_set_skip_total_records ()
46+ {
47+ DB ::enableQueryLog ();
48+
49+ $ crawler = $ this ->call ('GET ' , '/skip-total-records ' );
3750 $ crawler ->assertJson ([
3851 'draw ' => 0 ,
3952 'recordsTotal ' => 0 ,
4053 'recordsFiltered ' => 20 ,
4154 ]);
55+
56+ DB ::disableQueryLog ();
57+ $ queryLog = DB ::getQueryLog ();
58+
59+ $ this ->assertCount (2 , $ queryLog );
4260 }
4361
4462 #[Test]
@@ -463,6 +481,10 @@ protected function setUp(): void
463481 ->setTotalRecords (0 )
464482 ->toJson ());
465483
484+ $ router ->get ('/skip-total-records ' , fn (DataTables $ dataTable ) => $ dataTable ->query (DB ::table ('users ' ))
485+ ->skipTotalRecords ()
486+ ->toJson ());
487+
466488 $ router ->get ('/set-filtered-records ' , fn (DataTables $ dataTable ) => $ dataTable ->query (DB ::table ('users ' ))
467489 ->setFilteredRecords (10 )
468490 ->toJson ());
You can’t perform that action at this time.
0 commit comments