Skip to content

Commit 2ed832c

Browse files
authored
Merge pull request #16 from iYogesharma/3.x
fixes #15
2 parents f2c40b5 + c390085 commit 2ed832c

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"datatable",
77
"laravel"
88
],
9-
"version": "3.3",
9+
"version": "3.3.1",
1010
"license": "MIT",
1111
"authors": [{
1212
"name": "Yogesh Sharma",

src/AbstractDatatable.php

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract class AbstractDatatable implements DatatableDriverInterface
3434
* @var int
3535
*/
3636
protected $order = 0;
37-
37+
3838
/**
3939
* Direction of sort asc/desc
4040
* @default direction ascending
@@ -70,21 +70,21 @@ abstract class AbstractDatatable implements DatatableDriverInterface
7070
* @var mixed
7171
*/
7272
protected $query;
73-
73+
7474
/**
7575
* Holds DatatableRequest Instance
7676
* @var DatatableRequest
7777
*/
7878
protected $request;
79-
79+
8080
/**
8181
* Initializes new instance
8282
*/
8383
public function __construct()
8484
{
8585
$this->request = new DatatableRequest();
8686
}
87-
87+
8888
/**
8989
* Initialize datatable
9090
* @param object $source instance of one of
@@ -101,10 +101,10 @@ public function datatable($source, $json = false)
101101
}
102102
// Set properties of class and initialize datatable
103103
$this->boot($source);
104-
104+
105105
return $json ? $this->jsonResponse() : $this;
106106
}
107-
107+
108108
/**
109109
* Initialize datatable
110110
* @param object $source instance of one of
@@ -124,13 +124,13 @@ public function makeDatatable($source)
124124

125125
return $this->jsonResponse();
126126
}
127-
127+
128128
/**
129129
* Set @property $query of class
130130
* @param instance $source
131131
*/
132132
abstract public function setQuery($source);
133-
133+
134134
/**
135135
* Initialize datatable buy setting all its
136136
* properties to be used throughout the
@@ -160,7 +160,7 @@ protected function setProperties()
160160
$this->order = $this->request->getOrderableColumnIndex();
161161
$this->dir = $this->request->getOrderDirection();
162162
}
163-
163+
164164
$this->setColumns();
165165
}
166166

@@ -217,9 +217,9 @@ protected function setWhereColumns()
217217
protected function prepareQuery()
218218
{
219219
$this->checkIfQueryIsForSearchingPurpose();
220-
220+
221221
$this->setTotalDataAndFiltered();
222-
222+
223223
if ($this->request->getPerPage() === "-1") {
224224
$this->prepareQueryWithoutOffset();
225225
} else {
@@ -243,17 +243,34 @@ protected function checkIfQueryIsForSearchingPurpose()
243243
$this->searchQuery();
244244
}
245245
}
246-
246+
247247
/**
248248
* Set @properties $totalData and $totalFiltered of class
249249
*
250250
* @returrn void
251251
*/
252252
protected function setTotalDataAndFiltered()
253253
{
254-
$this->totalData = $this->totalData ?? $this->query->count();
255-
$this->totalFiltered = $this->query->count();
254+
if( ! $this->totalData )
255+
{
256+
// to get correct result count in case of group by
257+
if( $this->query->groups )
258+
{
259+
$this->totalData = $this->query->getCountForPagination();
260+
}
261+
else
262+
{
263+
$this->totalData = $this->query->count();
264+
}
265+
266+
$this->totalFiltered = $this->totalData;
267+
}
268+
else
269+
{
270+
$this->totalFiltered = $this->query->count();
271+
}
256272
}
273+
257274
/**
258275
* Prepare result to return as response
259276
*
@@ -262,7 +279,7 @@ protected function setTotalDataAndFiltered()
262279
protected function prepareQueryWithoutOffset()
263280
{
264281
$this->query = $this->query->orderBy($this->columns[$this->order],$this->dir);
265-
282+
266283
$this->result = $this->query->get();
267284
}
268285

@@ -303,7 +320,7 @@ protected function searchQuery()
303320
}
304321

305322
}
306-
323+
307324
/**
308325
* Apply conditions on query
309326
* @param string $search
@@ -359,7 +376,7 @@ public function response()
359376
];
360377

361378
}
362-
379+
363380
/**
364381
* Return data to initialise datatable
365382
*
@@ -368,7 +385,7 @@ public function response()
368385
public function jsonResponse()
369386
{
370387
return json_encode($this->response());
371-
388+
372389
}
373390

374391
/**
@@ -440,7 +457,7 @@ public function addColumns(array $column)
440457
}
441458
return $this;
442459
}
443-
460+
444461
/**
445462
* Add/edit details of multiple columns of datatable
446463
*
@@ -452,7 +469,7 @@ public function editColumns(array $column)
452469
{
453470
return $this->addColumns($column);
454471
}
455-
472+
456473
/**
457474
* Get Datatable query result
458475
* @return mixed
@@ -461,7 +478,7 @@ public function getResult()
461478
{
462479
return $this->result;
463480
}
464-
481+
465482
/**
466483
* Get Datatable query builder instance
467484
* @return mixed

0 commit comments

Comments
 (0)