From cb0a50233c28382ec88c194aa71375a45fb8caba Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Thu, 3 Jan 2019 16:54:13 +0530 Subject: [PATCH] Datagrid now fault tolerant on not passing select key in its configuration --- config/datagrid.php | 7 +- .../Admin/src/DataGrids/CategoryDataGrid.php | 3 +- packages/Webkul/Ui/src/DataGrid/DataGrid.php | 65 ++++++++++++++----- .../Ui/src/DataGrid/Helpers/Pagination.php | 2 +- 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/config/datagrid.php b/config/datagrid.php index 8a99c7595..19d690e0e 100644 --- a/config/datagrid.php +++ b/config/datagrid.php @@ -2,12 +2,17 @@ return [ + /** + * Default Select Value + */ + 'select' => 'id', + /** * Default OrderBy * * * Accepted Values = Array */ - 'defaultOrder' => [ + 'order' => [ 'column' => 'id', 'direction' => 'desc' ], diff --git a/packages/Webkul/Admin/src/DataGrids/CategoryDataGrid.php b/packages/Webkul/Admin/src/DataGrids/CategoryDataGrid.php index e44ac9269..e4323eb02 100644 --- a/packages/Webkul/Admin/src/DataGrids/CategoryDataGrid.php +++ b/packages/Webkul/Admin/src/DataGrids/CategoryDataGrid.php @@ -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' =>[ // [ diff --git a/packages/Webkul/Ui/src/DataGrid/DataGrid.php b/packages/Webkul/Ui/src/DataGrid/DataGrid.php index f94526732..81764dd19 100644 --- a/packages/Webkul/Ui/src/DataGrid/DataGrid.php +++ b/packages/Webkul/Ui/src/DataGrid/DataGrid.php @@ -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) { diff --git a/packages/Webkul/Ui/src/DataGrid/Helpers/Pagination.php b/packages/Webkul/Ui/src/DataGrid/Helpers/Pagination.php index f1f276680..1cd6f044e 100644 --- a/packages/Webkul/Ui/src/DataGrid/Helpers/Pagination.php +++ b/packages/Webkul/Ui/src/DataGrid/Helpers/Pagination.php @@ -17,7 +17,7 @@ use Illuminate\Pagination\Paginator; class Pagination extends Paginator { - const LIMIT = 20; + const LIMIT = 10; const OFFSET = 0; const VIEW = '';