Datagrid now fault tolerant on not passing select key in its configuration

This commit is contained in:
Prashant Singh 2019-01-03 16:54:13 +05:30
parent 7117038287
commit cb0a50233c
4 changed files with 55 additions and 22 deletions

View File

@ -2,12 +2,17 @@
return [
/**
* Default Select Value
*/
'select' => 'id',
/**
* Default OrderBy
*
* * Accepted Values = Array
*/
'defaultOrder' => [
'order' => [
'column' => 'id',
'direction' => 'desc'
],

View File

@ -24,10 +24,9 @@ class CategoryDataGrid
return DataGrid::make([
'name' => 'Categories',
'table' => 'categories as cat',
'select' => 'cat.id',
'select' => '',
'perpage' => 10,
'aliased' => true, //use this with false as default and true in case of joins
'alias' => 'cat',
'massoperations' =>[
// [

View File

@ -167,7 +167,7 @@ class DataGrid
}
//This assigns the private and public properties of the datagrid classes from make functions
public function build(
public function build (
string $name = null,
string $select = null,
array $filterable = [],
@ -185,12 +185,12 @@ class DataGrid
$this->request = Request::capture();
$this->setName($name);
$this->setAlias($aliased);
$this->setTable($table);
$this->setSelect($select);
$this->setFilterable($filterable);
$this->setSearchable($filterable);
$this->setMassOperations($massoperations);
$this->setPerPage($perpage);
$this->setTable($table);
$this->setJoin($join);
$this->addColumns($columns, true);
$this->setCss($css);
@ -219,7 +219,18 @@ class DataGrid
*/
public function setSelect($select)
{
$this->select = $select ? : false;
if (!$select) {
$select = config('datagrid.select');
if ($this->aliased) {
$alias = $this->findTableAlias();
$select = $alias.'.'.$select;
}
}
$this->select = $select;
return $this;
}
@ -347,6 +358,21 @@ class DataGrid
// return $this;
// }
/**
* get alias of the leftmost table
*
* @return null|string
*/
public function findTableAlias() {
if($this->aliased) {
$alias = trim(explode('as', $this->table)[1]);
} else {
$alias = null;
}
return $alias;
}
/**
* Section actions bag here.
*
@ -367,11 +393,13 @@ class DataGrid
if ($reCreate) {
$this->columns = new Collection();
}
if ($columns) {
foreach ($columns as $column) {
$this->addColumn($column);
}
}
return $this;
}
@ -440,27 +468,28 @@ class DataGrid
} else {
throw new \Exception("DataGrid: Pagination argument is not valid!");
}
return $this;
}
/**
* Used for selecting
* the columns got in
* make from controller.
* Used for selecting the columns got in make from controller.
*
* @return $this
*/
private function getSelect()
{
$select = [];
if ($this->select) {
$this->query->addselect($this->select);
}
foreach ($this->columns as $column) {
$this->query->addselect(DB::raw($column->name.' as '.$column->alias));
}
// $this->query->select(...$select);
if ($this->select) {
$this->query->addselect($this->select);
}
}
/**
@ -470,7 +499,7 @@ class DataGrid
* name.
* @return string
*/
public function findAlias($column_alias) {
public function findColumnAlias($column_alias) {
foreach($this->columns as $column) {
if($column->alias == $column_alias) {
return $column->name;
@ -485,7 +514,7 @@ class DataGrid
* name.
* @return string
*/
public function findType($column_alias) {
public function findColumnType($column_alias) {
foreach($this->columns as $column) {
if($column->alias == $column_alias) {
return $column->type;
@ -564,13 +593,13 @@ class DataGrid
if ($this->aliased) { //aliasing is expected in this case or it will be changed to presence of join bag
foreach ($parsed as $key=>$value) {
$column_name = $this->findAlias($key);
$column_type = $this->findType($key);
$column_name = $this->findColumnAlias($key);
$column_type = $this->findColumnType($key);
if ($key == "sort") {
//resolve the case with the column helper class
if(substr_count($key,'_') >= 1)
$column_name = $this->findAlias($key);
$column_name = $this->findColumnAlias($key);
//case that don't need any resolving
$count_keys = count(array_keys($value));
@ -592,7 +621,7 @@ class DataGrid
}
});
} else {
$column_name = $this->findAlias($key);
$column_name = $this->findColumnAlias($key);
if (array_keys($value)[0]=="like" || array_keys($value)[0]=="nlike") {
foreach ($value as $condition => $filter_value) {
$this->query->where(
@ -650,8 +679,8 @@ class DataGrid
} else {
// $column_name = $key;
$column_name = $this->findAlias($key);
$column_type = $this->findType($key);
$column_name = $this->findColumnAlias($key);
$column_type = $this->findColumnType($key);
if (array_keys($value)[0]=="like" || array_keys($value)[0]=="nlike") {
foreach ($value as $condition => $filter_value) {

View File

@ -17,7 +17,7 @@ use Illuminate\Pagination\Paginator;
class Pagination extends Paginator
{
const LIMIT = 20;
const LIMIT = 10;
const OFFSET = 0;
const VIEW = '';