refactoring
This commit is contained in:
parent
9bf7810a5f
commit
15d9155a9d
|
|
@ -209,33 +209,37 @@ abstract class DataGrid
|
|||
/**
|
||||
* Add the index as alias of the column and use the column to make things happen
|
||||
*
|
||||
* @param string $alias
|
||||
* @param string $column
|
||||
* @param string $alias
|
||||
* @param string $column
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addFilter($alias, $column) {
|
||||
public function addFilter($alias, $column)
|
||||
{
|
||||
$this->filterMap[$alias] = $column;
|
||||
|
||||
$this->enableFilterMap = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @param string $column
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addColumn($column)
|
||||
{
|
||||
$this->fireEvent('add.column.before.'.$column['index']);
|
||||
$this->fireEvent('add.column.before.' . $column['index']);
|
||||
|
||||
array_push($this->columns, $column);
|
||||
|
||||
$this->setCompleteColumnDetails($column);
|
||||
|
||||
$this->fireEvent('add.column.after.'.$column['index']);
|
||||
$this->fireEvent('add.column.after.' . $column['index']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @param string $column
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setCompleteColumnDetails($column)
|
||||
|
|
@ -244,7 +248,8 @@ abstract class DataGrid
|
|||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Query\Builder $queryBuilder
|
||||
* @param \Illuminate\Database\Query\Builder $queryBuilder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setQueryBuilder($queryBuilder)
|
||||
|
|
@ -253,7 +258,8 @@ abstract class DataGrid
|
|||
}
|
||||
|
||||
/**
|
||||
* @param array $action
|
||||
* @param array $action
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addAction($action)
|
||||
|
|
@ -266,7 +272,7 @@ abstract class DataGrid
|
|||
$eventName = null;
|
||||
}
|
||||
|
||||
$this->fireEvent('action.before.'.$eventName);
|
||||
$this->fireEvent('action.before.' . $eventName);
|
||||
|
||||
array_push($this->actions, $action);
|
||||
|
||||
|
|
@ -276,7 +282,8 @@ abstract class DataGrid
|
|||
}
|
||||
|
||||
/**
|
||||
* @param array $massAction
|
||||
* @param array $massAction
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addMassAction($massAction)
|
||||
|
|
@ -306,21 +313,24 @@ abstract class DataGrid
|
|||
$parsedUrl = $this->parseUrl();
|
||||
|
||||
foreach ($parsedUrl as $key => $value) {
|
||||
if ( $key == 'locale') {
|
||||
if ( ! is_array($value)) {
|
||||
if ($key == 'locale') {
|
||||
if (! is_array($value)) {
|
||||
unset($parsedUrl[$key]);
|
||||
}
|
||||
} elseif ( ! is_array($value)) {
|
||||
} elseif (! is_array($value)) {
|
||||
unset($parsedUrl[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($parsedUrl)) {
|
||||
$filteredOrSortedCollection = $this->sortOrFilterCollection($this->collection = $this->queryBuilder, $parsedUrl);
|
||||
$filteredOrSortedCollection = $this->sortOrFilterCollection($this->collection = $this->queryBuilder,
|
||||
$parsedUrl);
|
||||
|
||||
if ($this->paginate) {
|
||||
if ($this->itemsPerPage > 0)
|
||||
return $filteredOrSortedCollection->orderBy($this->index, $this->sortOrder)->paginate($this->itemsPerPage)->appends(request()->except('page'));
|
||||
if ($this->itemsPerPage > 0) {
|
||||
return $filteredOrSortedCollection->orderBy($this->index,
|
||||
$this->sortOrder)->paginate($this->itemsPerPage)->appends(request()->except('page'));
|
||||
}
|
||||
} else {
|
||||
return $filteredOrSortedCollection->orderBy($this->index, $this->sortOrder)->get();
|
||||
}
|
||||
|
|
@ -328,7 +338,8 @@ abstract class DataGrid
|
|||
|
||||
if ($this->paginate) {
|
||||
if ($this->itemsPerPage > 0) {
|
||||
$this->collection = $this->queryBuilder->orderBy($this->index, $this->sortOrder)->paginate($this->itemsPerPage)->appends(request()->except('page'));
|
||||
$this->collection = $this->queryBuilder->orderBy($this->index,
|
||||
$this->sortOrder)->paginate($this->itemsPerPage)->appends(request()->except('page'));
|
||||
}
|
||||
} else {
|
||||
$this->collection = $this->queryBuilder->orderBy($this->index, $this->sortOrder)->get();
|
||||
|
|
@ -340,26 +351,28 @@ abstract class DataGrid
|
|||
/**
|
||||
* To find the alias of the column and by taking the column name.
|
||||
*
|
||||
* @param array $columnAlias
|
||||
* @param array $columnAlias
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function findColumnType($columnAlias)
|
||||
{
|
||||
foreach($this->completeColumnDetails as $column) {
|
||||
if($column['index'] == $columnAlias) {
|
||||
foreach ($this->completeColumnDetails as $column) {
|
||||
if ($column['index'] == $columnAlias) {
|
||||
return [$column['type'], $column['index']];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Support\Collection $collection
|
||||
* @param array $parseInfo
|
||||
* @param \Illuminate\Support\Collection $collection
|
||||
* @param array $parseInfo
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function sortOrFilterCollection($collection, $parseInfo)
|
||||
{
|
||||
foreach ($parseInfo as $key => $info) {
|
||||
foreach ($parseInfo as $key => $info) {
|
||||
$columnType = $this->findColumnType($key)[0] ?? null;
|
||||
$columnName = $this->findColumnType($key)[1] ?? null;
|
||||
|
||||
|
|
@ -384,15 +397,16 @@ abstract class DataGrid
|
|||
}
|
||||
|
||||
if ($count_keys == 1) {
|
||||
$collection->where(function($collection) use($info) {
|
||||
$collection->where(function ($collection) use ($info) {
|
||||
foreach ($this->completeColumnDetails as $column) {
|
||||
if ($column['searchable'] == true) {
|
||||
if($this->enableFilterMap && isset($this->filterMap[$column['index']])) {
|
||||
$collection->orWhere($this->filterMap[$column['index']], 'like', '%'.$info['all'].'%');
|
||||
} elseif($this->enableFilterMap && !isset($this->filterMap[$column['index']])) {
|
||||
$collection->orWhere($column['index'], 'like', '%'.$info['all'].'%');
|
||||
}else {
|
||||
$collection->orWhere($column['index'], 'like', '%'.$info['all'].'%');
|
||||
if ($this->enableFilterMap && isset($this->filterMap[$column['index']])) {
|
||||
$collection->orWhere($this->filterMap[$column['index']], 'like',
|
||||
'%' . $info['all'] . '%');
|
||||
} elseif ($this->enableFilterMap && ! isset($this->filterMap[$column['index']])) {
|
||||
$collection->orWhere($column['index'], 'like', '%' . $info['all'] . '%');
|
||||
} else {
|
||||
$collection->orWhere($column['index'], 'like', '%' . $info['all'] . '%');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -400,7 +414,7 @@ abstract class DataGrid
|
|||
}
|
||||
} else {
|
||||
foreach ($this->completeColumnDetails as $column) {
|
||||
if($column['index'] === $columnName && !$column['filterable']) {
|
||||
if ($column['index'] === $columnName && ! $column['filterable']) {
|
||||
return $collection;
|
||||
}
|
||||
}
|
||||
|
|
@ -411,19 +425,19 @@ abstract class DataGrid
|
|||
$collection->where(
|
||||
$this->filterMap[$columnName],
|
||||
$this->operators[$condition],
|
||||
'%'.$filter_value.'%'
|
||||
'%' . $filter_value . '%'
|
||||
);
|
||||
} elseif ($this->enableFilterMap && ! isset($this->filterMap[$columnName])) {
|
||||
$collection->where(
|
||||
$columnName,
|
||||
$this->operators[$condition],
|
||||
'%'.$filter_value.'%'
|
||||
'%' . $filter_value . '%'
|
||||
);
|
||||
} else {
|
||||
$collection->where(
|
||||
$columnName,
|
||||
$this->operators[$condition],
|
||||
'%'.$filter_value.'%'
|
||||
'%' . $filter_value . '%'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -461,7 +475,7 @@ abstract class DataGrid
|
|||
$this->operators[$condition],
|
||||
$filter_value
|
||||
);
|
||||
} elseif($this->enableFilterMap && !isset($this->filterMap[$columnName])) {
|
||||
} elseif ($this->enableFilterMap && ! isset($this->filterMap[$columnName])) {
|
||||
$collection->where(
|
||||
$columnName,
|
||||
$this->operators[$condition],
|
||||
|
|
@ -484,7 +498,8 @@ abstract class DataGrid
|
|||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $name
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function fireEvent($name)
|
||||
|
|
|
|||
|
|
@ -236,7 +236,7 @@
|
|||
getColumnOrAlias: function(columnOrAlias) {
|
||||
this.columnOrAlias = columnOrAlias;
|
||||
|
||||
for(column in this.columns) {
|
||||
for (column in this.columns) {
|
||||
if (this.columns[column].index === this.columnOrAlias) {
|
||||
this.type = this.columns[column].type;
|
||||
|
||||
|
|
@ -290,8 +290,8 @@
|
|||
getResponse: function() {
|
||||
label = '';
|
||||
|
||||
for(let colIndex in this.columns) {
|
||||
if(this.columns[colIndex].index == this.columnOrAlias) {
|
||||
for (let colIndex in this.columns) {
|
||||
if (this.columns[colIndex].index == this.columnOrAlias) {
|
||||
label = this.columns[colIndex].label;
|
||||
break;
|
||||
}
|
||||
|
|
@ -309,8 +309,9 @@
|
|||
alert('{{__('ui::app.datagrid.zero-index')}}');
|
||||
}
|
||||
|
||||
if(indexConditions)
|
||||
if (indexConditions) {
|
||||
this.formURL(this.columnOrAlias, this.numberCondition, this.numberValue, label);
|
||||
}
|
||||
} else if (this.type === 'boolean') {
|
||||
this.formURL(this.columnOrAlias, this.booleanCondition, this.booleanValue, label);
|
||||
} else if (this.type === 'datetime') {
|
||||
|
|
@ -323,8 +324,8 @@
|
|||
sortCollection: function(alias) {
|
||||
let label = '';
|
||||
|
||||
for(let colIndex in this.columns) {
|
||||
if(this.columns[colIndex].index === alias) {
|
||||
for (let colIndex in this.columns) {
|
||||
if (this.columns[colIndex].index === alias) {
|
||||
matched = 0;
|
||||
label = this.columns[colIndex].label;
|
||||
break;
|
||||
|
|
@ -346,7 +347,7 @@
|
|||
this.arrayFromUrl();
|
||||
}
|
||||
|
||||
for(let id in this.massActions) {
|
||||
for (let id in this.massActions) {
|
||||
targetObj = {
|
||||
'type': this.massActions[id].type,
|
||||
'action': this.massActions[id].action
|
||||
|
|
@ -363,7 +364,7 @@
|
|||
},
|
||||
|
||||
findCurrentSort: function() {
|
||||
for(let i in this.filters) {
|
||||
for (let i in this.filters) {
|
||||
if (this.filters[i].column === 'sort') {
|
||||
this.currentSort = this.filters[i].val;
|
||||
}
|
||||
|
|
@ -372,7 +373,7 @@
|
|||
|
||||
changeMassActionTarget: function() {
|
||||
if (this.massActionType === 'delete') {
|
||||
for(let i in this.massActionTargets) {
|
||||
for (let i in this.massActionTargets) {
|
||||
if (this.massActionTargets[i].type === 'delete') {
|
||||
this.massActionTarget = this.massActionTargets[i].action;
|
||||
|
||||
|
|
@ -382,7 +383,7 @@
|
|||
}
|
||||
|
||||
if (this.massActionType === 'update') {
|
||||
for(let i in this.massActionTargets) {
|
||||
for (let i in this.massActionTargets) {
|
||||
if (this.massActionTargets[i].type === 'update') {
|
||||
this.massActionTarget = this.massActionTargets[i].action;
|
||||
|
||||
|
|
@ -410,13 +411,13 @@
|
|||
if (column !== "sort" && column !== "search") {
|
||||
let filterRepeated = false;
|
||||
|
||||
for(let j = 0; j < this.filters.length; j++) {
|
||||
for (let j = 0; j < this.filters.length; j++) {
|
||||
if (this.filters[j].column === column) {
|
||||
if (this.filters[j].cond === condition && this.filters[j].val === response) {
|
||||
filterRepeated = true;
|
||||
|
||||
return false;
|
||||
} else if(this.filters[j].cond === condition && this.filters[j].val !== response) {
|
||||
} else if (this.filters[j].cond === condition && this.filters[j].val !== response) {
|
||||
filterRepeated = true;
|
||||
|
||||
this.filters[j].val = response;
|
||||
|
|
@ -493,7 +494,7 @@
|
|||
if (column === "search") {
|
||||
let search_found = false;
|
||||
|
||||
for(let j = 0; j < this.filters.length; j++) {
|
||||
for (let j = 0; j < this.filters.length; j++) {
|
||||
if (this.filters[j].column === "search") {
|
||||
this.filters[j].column = column;
|
||||
this.filters[j].cond = condition;
|
||||
|
|
@ -581,11 +582,11 @@
|
|||
|
||||
splitted = processedUrl.split('&');
|
||||
|
||||
for(let i = 0; i < splitted.length; i++) {
|
||||
for (let i = 0; i < splitted.length; i++) {
|
||||
moreSplitted.push(splitted[i].split('='));
|
||||
}
|
||||
|
||||
for(let i = 0; i < moreSplitted.length; i++) {
|
||||
for (let i = 0; i < moreSplitted.length; i++) {
|
||||
const key = decodeURI(moreSplitted[i][0]);
|
||||
const value = decodeURI(moreSplitted[i][1]);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue