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
68
68
*/
69
69
protected ?int $ filteredRecords = null ;
70
70
71
+ /**
72
+ * Flag to check if the total records count should be skipped.
73
+ */
74
+ protected bool $ skipTotalRecords = false ;
75
+
71
76
/**
72
77
* Auto-filter flag.
73
78
*/
@@ -533,12 +538,11 @@ public function setTotalRecords(int $total): static
533
538
* This will improve the performance by skipping the total count query.
534
539
*
535
540
* @return $this
536
- *
537
- * @deprecated Just use setTotalRecords instead.
538
541
*/
539
542
public function skipTotalRecords (): static
540
543
{
541
544
$ this ->totalRecords = 0 ;
545
+ $ this ->skipTotalRecords = true ;
542
546
543
547
return $ this ;
544
548
}
Original file line number Diff line number Diff line change @@ -25,11 +25,6 @@ class QueryDataTable extends DataTableAbstract
25
25
*/
26
26
protected bool $ prepared = false ;
27
27
28
- /**
29
- * Flag to check if the total records count query has been performed.
30
- */
31
- protected bool $ performedTotalRecordsCount = false ;
32
-
33
28
/**
34
29
* Query callback for custom pagination using limit without offset.
35
30
*
@@ -162,20 +157,6 @@ public function prepareQuery(): static
162
157
return $ this ;
163
158
}
164
159
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
-
179
160
/**
180
161
* Counts current query.
181
162
*/
@@ -272,7 +253,7 @@ protected function filterRecords(): void
272
253
273
254
// If no modification between the original query and the filtered one has been made
274
255
// the filteredRecords equals the totalRecords
275
- if ($ this ->query == $ initialQuery && $ this ->performedTotalRecordsCount ) {
256
+ if (! $ this ->skipTotalRecords && $ this ->query == $ initialQuery ) {
276
257
$ this ->filteredRecords ??= $ this ->totalRecords ;
277
258
} else {
278
259
$ this ->filteredCount ();
Original file line number Diff line number Diff line change @@ -26,19 +26,37 @@ public function it_can_set_total_records()
26
26
$ crawler ->assertJson ([
27
27
'draw ' => 0 ,
28
28
'recordsTotal ' => 10 ,
29
- 'recordsFiltered ' => 20 ,
29
+ 'recordsFiltered ' => 10 ,
30
30
]);
31
31
}
32
32
33
33
#[Test]
34
34
public function it_can_set_zero_total_records ()
35
35
{
36
36
$ 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 ' );
37
50
$ crawler ->assertJson ([
38
51
'draw ' => 0 ,
39
52
'recordsTotal ' => 0 ,
40
53
'recordsFiltered ' => 20 ,
41
54
]);
55
+
56
+ DB ::disableQueryLog ();
57
+ $ queryLog = DB ::getQueryLog ();
58
+
59
+ $ this ->assertCount (2 , $ queryLog );
42
60
}
43
61
44
62
#[Test]
@@ -463,6 +481,10 @@ protected function setUp(): void
463
481
->setTotalRecords (0 )
464
482
->toJson ());
465
483
484
+ $ router ->get ('/skip-total-records ' , fn (DataTables $ dataTable ) => $ dataTable ->query (DB ::table ('users ' ))
485
+ ->skipTotalRecords ()
486
+ ->toJson ());
487
+
466
488
$ router ->get ('/set-filtered-records ' , fn (DataTables $ dataTable ) => $ dataTable ->query (DB ::table ('users ' ))
467
489
->setFilteredRecords (10 )
468
490
->toJson ());
You can’t perform that action at this time.
0 commit comments