Skip to content

Commit

Permalink
修改Grid Hasone关联模型排序
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallRuralDog committed Mar 25, 2020
1 parent 86779db commit 27e9987
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 12 deletions.
4 changes: 2 additions & 2 deletions resources/js/components/grid/Tree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<el-card shadow="never" :body-style="{ padding: 0 }">
<div class="grid-top-container">
<div class="grid-top-container-left">
<div class="search-view mr-10">
<div class="search-view mr-10" v-if="attrs.quickSearch">
<el-input
v-model="quickSearch"
size="medium"
:placeholder="attrs.quickSearch.placeholder"
:clearable="true"
@clear="getData"
v-if="attrs.quickSearch"

@focus="onQuickSearchFocus"
@blur="onQuickSearchBlur"
>
Expand Down
2 changes: 1 addition & 1 deletion src/Grid/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public function customValueUsing($row, $value)

/**
* 设置组件
* @deprecated
* @param $component
* @return $this
* @deprecated
*/
public function displayComponent($component)
{
Expand Down
128 changes: 119 additions & 9 deletions src/Grid/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -222,7 +224,6 @@ protected function setSort()
$type = request('sort_order', null);



if ($sort_field && in_array($type, ['asc', 'desc'])) {
$this->sort = [
'column' => $sort_field,
Expand All @@ -246,9 +247,10 @@ protected function setSort()
}

if (Str::contains($this->sort['column'], '.')) {
//$this->setRelationSort($this->sort['column']);
$this->setRelationSort($this->sort['column']);

} else {
//$this->resetOrderBy();
$this->resetOrderBy();

// get column. if contains "cast", set set column as cast
if (!empty($this->sort['cast'])) {
Expand All @@ -269,10 +271,102 @@ protected function setSort()
}
}

/**
* @param $column
* @throws \Exception
*/
protected function setRelationSort($column)
{
list($relationName, $relationColumn) = explode('.', $column);


if ($this->queries->contains(function ($query) use ($relationName) {
return $query['method'] == 'with' && in_array($relationName, $query['arguments']);
})) {
$relation = $this->model->$relationName();


$this->queries->push([
'method' => 'select',
'arguments' => [$this->model->getTable() . '.*'],
]);


$this->queries->push([
'method' => 'join',
'arguments' => $this->joinParameters($relation),
]);

$this->resetOrderBy();

$this->queries->push([
'method' => 'orderBy',
'arguments' => [
$relation->getRelated()->getTable() . '.' . $relationColumn,
$this->sort['type'],
],
]);
}
}

public function resetOrderBy()
{
$this->queries = $this->queries->reject(function ($query) {
return $query['method'] == 'orderBy' || $query['method'] == 'orderByDesc';
});
}

/**
* @param Relation $relation
* @return array
* @throws \Exception
*/
protected function joinParameters(Relation $relation)
{
$relatedTable = $relation->getRelated()->getTable();

if ($relation instanceof BelongsTo) {
$foreignKeyMethod = version_compare(app()->version(), '5.8.0', '<') ? 'getForeignKey' : 'getForeignKeyName';


return [
$relatedTable,
$relation->{$foreignKeyMethod}(),
'=',
$relatedTable . '.' . $relation->getRelated()->getKeyName(),
];
}

if ($relation instanceof HasOne) {

return [
$relatedTable, function ($join) use ($relation) {
$join->on($relation->getQualifiedParentKeyName(), "=", $relation->getQualifiedForeignKeyName())
->where(function (\Illuminate\Database\Query\Builder $query) use ($relation) {
collect($relation->getBaseQuery()->wheres)->filter(function ($item) {
return $item['value'] ?? false;
})->each(function ($item) use ($query) {
$query->where($item['column'], $item['value']);
});
});
}
];

/* return [
$relatedTable,
$relation->getQualifiedParentKeyName(),
'=',
$relation->getQualifiedForeignKeyName(),
];*/
}

throw new \Exception('Related sortable only support `HasOne` and `BelongsTo` relation.');
}

protected function handleInvalidPage(LengthAwarePaginator $paginator)
{
if ($paginator->lastPage() && $paginator->currentPage() > $paginator->lastPage()) {
$lastPageUrl = Request::fullUrlWithQuery([
$lastPageUrl = \Request::fullUrlWithQuery([
$paginator->getPageName() => $paginator->lastPage(),
]);
}
Expand All @@ -298,19 +392,36 @@ public function buildData($toArray = false)
protected function displayData($data)
{
$columcs = $this->grid->getColumns();
$items = [];
foreach ($data as $row) {
$item = collect($row)->toArray();
foreach ($columcs as $column) {
$n_value = $column->customValueUsing($row, Arr::get($row, $column->getName()));
Arr::set($item, $column->getName(), $n_value);
}
$items[] = $item;
}

return $items;


/*$data = collect($data)->map(function ($row) use ($columcs) {
$data = collect($data)->map(function ($row) use ($columcs) {
collect($columcs)->each(function (Column $column) use ($row) {
$keys = explode(".", $column->getName());
$keys = array_filter($keys);
$keys = array_unique($keys);
if (count($keys) > 0) {
$value = $row[$keys[0]];
$row[$keys[0]] = $column->customValueUsing($row, $value);
$n_value = $column->customValueUsing($row, $value);
Arr::set($row, $column->getName(), $n_value);
}
});
return $row;
})->toArray();
})->toArray();*/


return $data;
Expand Down Expand Up @@ -350,7 +461,6 @@ public function get()
$this->setSort();
$this->setPaginate();

//dd($this->queries);

$this->queries->unique()->each(function ($query) {
$this->model = call_user_func_array([$this->model, $query['method']], $query['arguments']);
Expand All @@ -360,7 +470,7 @@ public function get()
$data = $this->model;

if ($this->model instanceof Collection) {
return $data;
return $this->displayData($data);
}

if ($this->model instanceof LengthAwarePaginator) {
Expand Down

0 comments on commit 27e9987

Please sign in to comment.