From f4f14c9f0fd8298138e9fed192da45d922df7a4f Mon Sep 17 00:00:00 2001 From: Yogesh Sharma Date: Thu, 3 Mar 2022 12:57:45 +0530 Subject: [PATCH] fixes #15 --- composer.json | 2 +- src/AbstractDatatable.php | 59 +++++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/composer.json b/composer.json index b347f9e..968a867 100755 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "datatable", "laravel" ], - "version": "3.2", + "version": "3.2.1", "license": "MIT", "authors": [{ "name": "Yogesh Sharma", diff --git a/src/AbstractDatatable.php b/src/AbstractDatatable.php index 8bc053f..86bf1bf 100755 --- a/src/AbstractDatatable.php +++ b/src/AbstractDatatable.php @@ -34,7 +34,7 @@ abstract class AbstractDatatable implements DatatableDriverInterface * @var int */ protected $order = 0; - + /** * Direction of sort asc/desc * @default direction ascending @@ -70,13 +70,13 @@ abstract class AbstractDatatable implements DatatableDriverInterface * @var mixed */ protected $query; - + /** * Holds DatatableRequest Instance * @var DatatableRequest */ protected $request; - + /** * Initializes new instance */ @@ -84,7 +84,7 @@ public function __construct() { $this->request = new DatatableRequest(); } - + /** * Initialize datatable * @param object $source instance of one of @@ -101,10 +101,10 @@ public function datatable($source, $json = false) } // Set properties of class and initialize datatable $this->boot($source); - + return $json ? $this->jsonResponse() : $this; } - + /** * Initialize datatable * @param object $source instance of one of @@ -124,13 +124,13 @@ public function makeDatatable($source) return $this->jsonResponse(); } - + /** * Set @property $query of class * @param instance $source */ abstract public function setQuery($source); - + /** * Initialize datatable buy setting all its * properties to be used throughout the @@ -160,7 +160,7 @@ protected function setProperties() $this->order = $this->request->getOrderableColumnIndex(); $this->dir = $this->request->getOrderDirection(); } - + $this->setColumns(); } @@ -217,9 +217,9 @@ protected function setWhereColumns() protected function prepareQuery() { $this->checkIfQueryIsForSearchingPurpose(); - + $this->setTotalDataAndFiltered(); - + if ($this->request->getPerPage() === "-1") { $this->prepareQueryWithoutOffset(); } else { @@ -243,7 +243,7 @@ protected function checkIfQueryIsForSearchingPurpose() $this->searchQuery(); } } - + /** * Set @properties $totalData and $totalFiltered of class * @@ -251,9 +251,26 @@ protected function checkIfQueryIsForSearchingPurpose() */ protected function setTotalDataAndFiltered() { - $this->totalData = $this->totalData ?? $this->query->count(); - $this->totalFiltered = $this->query->count(); + if( ! $this->totalData ) + { + // to get correct result count in case of group by + if( $this->query->groups ) + { + $this->totalData = $this->query->getCountForPagination(); + } + else + { + $this->totalData = $this->query->count(); + } + + $this->totalFiltered = $this->totalData; + } + else + { + $this->totalFiltered = $this->query->count(); + } } + /** * Prepare result to return as response * @@ -262,7 +279,7 @@ protected function setTotalDataAndFiltered() protected function prepareQueryWithoutOffset() { $this->query = $this->query->orderBy($this->columns[$this->order],$this->dir); - + $this->result = $this->query->get(); } @@ -303,7 +320,7 @@ protected function searchQuery() } } - + /** * Apply conditions on query * @param string $search @@ -359,7 +376,7 @@ public function response() ]; } - + /** * Return data to initialise datatable * @@ -368,7 +385,7 @@ public function response() public function jsonResponse() { return json_encode($this->response()); - + } /** @@ -440,7 +457,7 @@ public function addColumns(array $column) } return $this; } - + /** * Add/edit details of multiple columns of datatable * @@ -452,7 +469,7 @@ public function editColumns(array $column) { return $this->addColumns($column); } - + /** * Get Datatable query result * @return mixed @@ -461,7 +478,7 @@ public function getResult() { return $this->result; } - + /** * Get Datatable query builder instance * @return mixed