AreaGrid.php
6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 管理端列表grid trait层:渲染生成 区域 grid 列表
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Grids\Area
* @package App\Admin\Grids\Area
* @author Richer <yangzi1028@163.com>
* @date 2021年1月27日11:18:22
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Grids\Area;
use App\Models\Area\Area;
use App\Models\Area\City;
use App\Models\Area\County;
use App\Models\Area\Group;
use App\Models\Area\Province;
use App\Models\Area\Township;
use App\Models\Area\Village;
use Encore\Admin\Grid;
/**
* Class AreaGrid.
*
* @category App\Admin\Grids\Area
* @package App\Admin\Grids\Area
* @author Richer <yangzi1028@163.com>
* @date 2021年1月27日11:18:22
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
trait AreaGrid
{
/**
* 设置默认查询条件
* 增加数据权限的判断。步骤:
* 1、判断用户是否是超级管理员,并判断用户的pid,如果pid为0 则为平台总用户,不进行数据权限的判断
* 2、如果pid大于0 则为 平台子管理员,则需要进行数据权限的判断
*
* @return void
*/
public function setGridQuery()
{
$with = [];
switch ($this->type) {
case Area::GROUP:
$with[] = 'village';
case Area::VILLAGE:
$with[] = 'township';
case Area::TOWNSHIP:
$with[] = 'county';
case Area::COUNTY:
$with[] = 'city';
case Area::CITY:
$with[] = 'province';
break;
}
if ($with) {
$this->grid->model()->with($with);
}
}
/**
* 设置默认排序规则
*
*/
public function setGridOrder()
{
if (!request('_sort')) {
switch ($this->type) {
case Area::GROUP:
$this->grid->model()->orderBy('village_id');
case Area::VILLAGE:
$this->grid->model()->orderBy('township_id');
case Area::TOWNSHIP:
$this->grid->model()->orderBy('county_id');
case Area::COUNTY:
$this->grid->model()->orderBy('city_id');
case Area::CITY:
$this->grid->model()->orderBy('province_id');
break;
}
$this->grid->model()->orderBy('sort')->latest();
}
}
/**
* 为grid增加筛选条件
*
* @return \Encore\Admin\Grid
*/
public function renderGridFilter()
{
$this->grid->selector(
function (Grid\Tools\Selector $selector) {
/* TODO 补充
$selector->select(
'price',
$this->title.'价位',
\Config::get('constants.price_range_options'),
function ($query, $value) {
$query->whereBetween('price', explode('-', Arr::first($value)));
}
);
*/
}
);
$this->grid->filter(
function ($filter) {
// 筛选条件默认展开
$filter->expand();
if ($this->level > Area::LEVEL_PROVINCE) {
$filter->equal('province_id')->select(Province::getSelectOptions())->load("city_id", "/admin/area/province/select-options")->placeholder(__(Province::OBJ_NAME));
}
if ($this->level > Area::LEVEL_CITY) {
$filter->equal('city_id')->select([])->load("county_id", "/admin/area/city/select-options")->placeholder(__(City::OBJ_NAME));
}
if ($this->level > Area::LEVEL_COUNTY) {
$filter->equal('county_id')->select([])->load("township_id", "/admin/area/county/select-options")->placeholder(__(County::OBJ_NAME));
}
if ($this->level > Area::LEVEL_TOWNSHIP) {
$filter->equal('township_id')->select([])->load("village_id", "/admin/area/township/select-options")->placeholder(__(Township::OBJ_NAME));
}
if ($this->level > Area::LEVEL_VILLAGE) {
$filter->equal('village_id')->select([])->placeholder(__(Village::OBJ_NAME));
}
$filter->like('name', __($this->title). ' ' . strtolower(__('name')));
//去掉默认的搜索
$filter->disableIdFilter();
}
);
return $this->grid;
}
/**
* 渲染grid字段
*
* @return void
*/
public function renderGridFields()
{
$this->gridRowNo();
// $this->gridTextField($this->grid, 'admin.username', trans('admin.username'));
if ($this->level > Area::LEVEL_PROVINCE) {
$this->gridTextField($this->grid, 'province_id', __(Province::OBJ_NAME))->display(function () {
return optional($this->province)->name;
});
}
if ($this->level > Area::LEVEL_CITY) {
$this->gridTextField($this->grid, 'city_id', __(City::OBJ_NAME))->display(function () {
return optional($this->city)->name;
});
}
if ($this->level > Area::LEVEL_COUNTY) {
$this->gridTextField($this->grid, 'county_id', __(County::OBJ_NAME))->display(function () {
return optional($this->county)->name;
});
}
if ($this->level > Area::LEVEL_TOWNSHIP) {
$this->gridTextField($this->grid, 'township_id', __(Township::OBJ_NAME))->display(function () {
return optional($this->township)->name;
});
}
if ($this->level > Area::LEVEL_VILLAGE) {
$this->gridTextField($this->grid, 'village_id', __(Village::OBJ_NAME))->display(function () {
return optional($this->village)->name;
});
}
$this->gridTextField($this->grid, 'name', __($this->title). ' ' . strtolower(__('name')))->sortable();
$this->gridNumberField($this->grid, 'sort', __('sort'))->editable()->sortable();
// $this->gridTextField($this->grid, 'phone', '办公电话');
// $this->gridTextField($this->grid, 'address', $this->title.'地址')->sortable();
$this->generateGridDateField($this->grid)->sortable();
}
}