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 [ return [
/**
* Default Select Value
*/
'select' => 'id',
/** /**
* Default OrderBy * Default OrderBy
* *
* * Accepted Values = Array * * Accepted Values = Array
*/ */
'defaultOrder' => [ 'order' => [
'column' => 'id', 'column' => 'id',
'direction' => 'desc' 'direction' => 'desc'
], ],

View File

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

View File

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

View File

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