@@ -72,16 +72,16 @@ abstract class DataTableAbstract implements DataTable
7272 /**
7373 * Total records.
7474 *
75- * @var int
75+ * @var int|null
7676 */
77- protected int $ totalRecords = 0 ;
77+ protected ? int $ totalRecords = null ;
7878
7979 /**
8080 * Total filtered records.
8181 *
82- * @var int
82+ * @var int|null
8383 */
84- protected int $ filteredRecords = 0 ;
84+ protected ? int $ filteredRecords = null ;
8585
8686 /**
8787 * Auto-filter flag.
@@ -109,17 +109,10 @@ abstract class DataTableAbstract implements DataTable
109109 'DT_RowAttr ' => [],
110110 ];
111111
112- /**
113- * [internal] Track if any filter was applied for at least one column.
114- *
115- * @var bool
116- */
117- protected bool $ isFilterApplied = false ;
118-
119112 /**
120113 * Custom ordering callback.
121114 *
122- * @var ? callable
115+ * @var callable|null
123116 */
124117 protected $ orderCallback = null ;
125118
@@ -453,7 +446,7 @@ public function with($key, $value = ''): static
453446 * @param callable $value
454447 * @return $this
455448 */
456- public function withQuery ($ key , callable $ value ): static
449+ public function withQuery (string $ key , callable $ value ): static
457450 {
458451 $ this ->appends [$ key ] = $ value ;
459452
@@ -492,7 +485,7 @@ public function blacklist(array $blacklist): static
492485 * @param string|array $whitelist
493486 * @return $this
494487 */
495- public function whitelist ($ whitelist = '* ' ): static
488+ public function whitelist (array | string $ whitelist = '* ' ): static
496489 {
497490 $ this ->columnDef ['whitelist ' ] = $ whitelist ;
498491
@@ -505,7 +498,7 @@ public function whitelist($whitelist = '*'): static
505498 * @param bool $state
506499 * @return $this
507500 */
508- public function smart ($ state = true ): static
501+ public function smart (bool $ state = true ): static
509502 {
510503 $ this ->config ->set ('datatables.search.smart ' , $ state );
511504
@@ -518,7 +511,7 @@ public function smart($state = true): static
518511 * @param bool $state
519512 * @return $this
520513 */
521- public function startsWithSearch ($ state = true ): static
514+ public function startsWithSearch (bool $ state = true ): static
522515 {
523516 $ this ->config ->set ('datatables.search.starts_with ' , $ state );
524517
@@ -531,7 +524,7 @@ public function startsWithSearch($state = true): static
531524 * @param bool $multiTerm
532525 * @return $this
533526 */
534- public function setMultiTerm ($ multiTerm = true ): static
527+ public function setMultiTerm (bool $ multiTerm = true ): static
535528 {
536529 $ this ->config ->set ('datatables.search.multi_term ' , $ multiTerm );
537530
@@ -544,20 +537,36 @@ public function setMultiTerm($multiTerm = true): static
544537 * @param int $total
545538 * @return $this
546539 */
547- public function setTotalRecords ($ total ): static
540+ public function setTotalRecords (int $ total ): static
548541 {
542+ $ this ->skipTotalRecords ();
549543 $ this ->totalRecords = $ total ;
550544
551545 return $ this ;
552546 }
553547
548+ /**
549+ * Skip total records and set the recordsTotal equals to recordsFiltered.
550+ * This will improve the performance by skipping the total count query.
551+ *
552+ * @return $this
553+ *
554+ * @deprecated Just use setTotalRecords instead.
555+ */
556+ public function skipTotalRecords (): static
557+ {
558+ $ this ->totalRecords = 0 ;
559+
560+ return $ this ;
561+ }
562+
554563 /**
555564 * Set filtered records manually.
556565 *
557566 * @param int $total
558567 * @return $this
559568 */
560- public function setFilteredRecords ($ total ): static
569+ public function setFilteredRecords (int $ total ): static
561570 {
562571 $ this ->filteredRecords = $ total ;
563572
@@ -651,7 +660,6 @@ abstract protected function defaultOrdering(): void;
651660 public function filter (callable $ callback , $ globalSearch = false ): static
652661 {
653662 $ this ->autoFilter = $ globalSearch ;
654- $ this ->isFilterApplied = true ;
655663 $ this ->filterCallback = $ callback ;
656664
657665 return $ this ;
@@ -721,7 +729,7 @@ protected function filterRecords(): void
721729
722730 $ this ->columnSearch ();
723731 $ this ->searchPanesSearch ();
724- $ this ->filteredRecords = $ this -> isFilterApplied ? $ this -> filteredCount () : $ this -> totalRecords ;
732+ $ this ->filteredCount ();
725733 }
726734
727735 /**
@@ -778,14 +786,24 @@ protected function searchPanesSearch(): void
778786 // Add support for search pane.
779787 }
780788
789+ /**
790+ * Count total items.
791+ *
792+ * @return int
793+ */
794+ public function totalCount (): int
795+ {
796+ return $ this ->totalRecords ??= $ this ->count ();
797+ }
798+
781799 /**
782800 * Count filtered items.
783801 *
784802 * @return int
785803 */
786804 protected function filteredCount (): int
787805 {
788- return $ this ->filteredRecords ?: $ this ->count ();
806+ return $ this ->filteredRecords ??= $ this ->count ();
789807 }
790808
791809 /**
0 commit comments