Skip to content

Commit

Permalink
Merge pull request #16 from iYogesharma/3.x
Browse files Browse the repository at this point in the history
fixes #15
  • Loading branch information
iYogesharma authored Mar 3, 2022
2 parents f2c40b5 + c390085 commit 2ed832c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"datatable",
"laravel"
],
"version": "3.3",
"version": "3.3.1",
"license": "MIT",
"authors": [{
"name": "Yogesh Sharma",
Expand Down
59 changes: 38 additions & 21 deletions src/AbstractDatatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class AbstractDatatable implements DatatableDriverInterface
* @var int
*/
protected $order = 0;

/**
* Direction of sort asc/desc
* @default direction ascending
Expand Down Expand Up @@ -70,21 +70,21 @@ abstract class AbstractDatatable implements DatatableDriverInterface
* @var mixed
*/
protected $query;

/**
* Holds DatatableRequest Instance
* @var DatatableRequest
*/
protected $request;

/**
* Initializes new instance
*/
public function __construct()
{
$this->request = new DatatableRequest();
}

/**
* Initialize datatable
* @param object $source instance of one of
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -160,7 +160,7 @@ protected function setProperties()
$this->order = $this->request->getOrderableColumnIndex();
$this->dir = $this->request->getOrderDirection();
}

$this->setColumns();
}

Expand Down Expand Up @@ -217,9 +217,9 @@ protected function setWhereColumns()
protected function prepareQuery()
{
$this->checkIfQueryIsForSearchingPurpose();

$this->setTotalDataAndFiltered();

if ($this->request->getPerPage() === "-1") {
$this->prepareQueryWithoutOffset();
} else {
Expand All @@ -243,17 +243,34 @@ protected function checkIfQueryIsForSearchingPurpose()
$this->searchQuery();
}
}

/**
* Set @properties $totalData and $totalFiltered of class
*
* @returrn void
*/
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
*
Expand All @@ -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();
}

Expand Down Expand Up @@ -303,7 +320,7 @@ protected function searchQuery()
}

}

/**
* Apply conditions on query
* @param string $search
Expand Down Expand Up @@ -359,7 +376,7 @@ public function response()
];

}

/**
* Return data to initialise datatable
*
Expand All @@ -368,7 +385,7 @@ public function response()
public function jsonResponse()
{
return json_encode($this->response());

}

/**
Expand Down Expand Up @@ -440,7 +457,7 @@ public function addColumns(array $column)
}
return $this;
}

/**
* Add/edit details of multiple columns of datatable
*
Expand All @@ -452,7 +469,7 @@ public function editColumns(array $column)
{
return $this->addColumns($column);
}

/**
* Get Datatable query result
* @return mixed
Expand All @@ -461,7 +478,7 @@ public function getResult()
{
return $this->result;
}

/**
* Get Datatable query builder instance
* @return mixed
Expand Down

0 comments on commit 2ed832c

Please sign in to comment.