Grid.php
5.5 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 重写laravel-admin Grid 类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Rewrite
* @package App\Admin\Rewrite
* @author Richer <yangzi1028@163.com>
* @date 2020年11月12日13:49:31
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Rewrite;
use App\Admin\Rewrite\Grid\Tools\CreateButton;
use App\Admin\Rewrite\Grid\Tools\ImportButton;
use Closure;
use App\Admin\Rewrite\Grid\Concerns;
use App\Admin\Rewrite\Grid\Tools\TrashedButton;
use Encore\Admin\Grid as EncoreGrid;
use App\Admin\Rewrite\Grid\Model;
use Illuminate\Database\Eloquent\Model as Eloquent;
/**
* Class Grid
*
* @category App\Admin\Rewrite
* @package App\Admin\Rewrite
* @author Richer <yangzi1028@163.com>
* @date 2020年11月12日13:49:31
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class Grid extends EncoreGrid
{
use Concerns\HasFilter,
Concerns\HasActions,
Concerns\HasTools;
/**
* Options for grid.
*
* @var array
*/
protected $options = [
'show_pagination' => true,
'show_tools' => true,
'show_filter' => true,
'show_exporter' => true,
'show_actions' => true,
'show_row_selector' => true,
'show_create_btn' => true,
'show_column_selector' => true,
'show_define_empty_page' => true,
'show_perpage_selector' => true,
'show_trashed_btn' => true,
'show_import_btn' => true,
];
/**
* Create a new grid instance.
*
* @param Eloquent $model
* @param Closure|null $builder
*/
public function __construct(Eloquent $model, Closure $builder = null)
{
$this->model = new Model($model, $this);
$this->keyName = $model->getKeyName();
$this->builder = $builder;
$this->initialize();
$this->callInitCallbacks();
}
/**
* Render create button for grid.
*
* @return string
*/
public function renderCreateButton()
{
return (new CreateButton($this))->render();
}
/**
* Alias for method `disableTrashedButton`.
*
* @return \Encore\Admin\Grid
*
* @deprecated
*/
public function disableTrashed()
{
return $this->disableTrashedButton();
}
/**
* Remove create button on grid.
*
* @return $this
*/
public function disableTrashedButton(bool $disable = true)
{
return $this->option('show_trashed_btn', !$disable);
}
/**
* If allow creation.
*
* @return bool
*/
public function showTrashedBtn()
{
return $this->option('show_trashed_btn');
}
/**
* Render create button for grid.
*
* @return string
*/
public function renderTrashedButton()
{
return (new TrashedButton($this))->render();
}
/**
* Get create url.
*
* @return string
*/
public function getTrashedUrl()
{
$queryString = '';
if ($constraints = $this->model()->getConstraints()) {
$queryString = http_build_query($constraints);
}
return sprintf(
'%s?trashed=1%s',
$this->resource(),
$queryString ? ('?'.$queryString) : ''
);
}
/**
* Alias for method `disableTrashedButton`.
*
* @return \Encore\Admin\Grid
*
* @deprecated
*/
public function disableImport()
{
return $this->disableImportButton();
}
/**
* Remove create button on grid.
*
* @param bool $disable
* @return $this
*/
public function disableImportButton(bool $disable = true)
{
return $this->option('show_import_btn', !$disable);
}
/**
* If allow Import.
*
* @return bool
*/
public function showImportBtn()
{
return $this->option('show_import_btn');
}
/**
* Render create button for grid.
*
* @return string
*/
public function renderImportButton()
{
return (new ImportButton($this))->render();
}
/**
* Get create url.
*
* @return string
*/
public function getImportUrl()
{
$queryString = '';
if ($constraints = $this->model()->getConstraints()) {
$queryString = http_build_query($constraints);
}
return sprintf(
'%s/import%s',
$this->resource(),
$queryString ? ('?'.$queryString) : ''
);
return sprintf(
'%s?=1%s',
$this->resource(),
$queryString ? ('?'.$queryString) : ''
);
}
/**
* Get create url.
*
* @return string
*/
public function getCommonUrl()
{
$queryString = '';
if ($constraints = $this->model()->getConstraints()) {
$queryString = http_build_query($constraints);
}
return sprintf(
'%s%s',
$this->resource(),
$queryString ? ('?'.$queryString) : ''
);
}
}