Skip to content

Commit f2c1c1e

Browse files
committed
Grid强化
1 parent a635f49 commit f2c1c1e

File tree

8 files changed

+352
-238
lines changed

8 files changed

+352
-238
lines changed

public/app.js

Lines changed: 239 additions & 196 deletions
Large diffs are not rendered by default.

resources/js/components/grid/Table.vue

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
:attrs="attrs.top"
77
/>
88

9-
<el-card shadow="never" :body-style="{ padding: 0 }">
9+
<el-card shadow="never" :body-style="{ padding: 0 }" v-if='attrs.toolbars.show'>
1010
<div class="grid-top-container">
1111
<div class="grid-top-container-left">
1212
<BatchActions
@@ -121,6 +121,7 @@
121121

122122
<div>
123123
<el-table
124+
:ref="attrs.ref || 'table'"
124125
:data="tableData"
125126
:row-key="attrs.attributes.rowKey"
126127
:default-sort="default_sort_get"
@@ -227,13 +228,15 @@
227228
</template>
228229

229230
<script>
231+
import { BaseComponent } from "@/mixins.js";
230232
import { mapState } from "vuex";
231233
import ColumnDisplay from "./ColumnDisplay";
232234
import Actions from "./Actions/Index";
233235
import BatchActions from "./BatchActions/Index";
234236
import ItemDiaplsy from "../form/ItemDiaplsy";
235237
import DialogForm from "./DialogForm";
236238
export default {
239+
mixins: [BaseComponent],
237240
components: {
238241
ColumnDisplay,
239242
Actions,
@@ -246,19 +249,19 @@ export default {
246249
},
247250
data() {
248251
return {
249-
loading: false,
250-
sort: {},
251-
tableData: [],
252+
loading: false,//是否加载
253+
sort: {},//排序对象
254+
tableData: [],//表格数据
252255
pageData: {
253256
pageSize: this.attrs.perPage,
254257
total: 0,
255258
currentPage: 1,
256259
lastPage: 1,
257260
},
258-
page: 1,
259-
quickSearch: null,
260-
selectionRows: [],
261-
filterFormData: null,
261+
page: 1,//当前页
262+
quickSearch: null,//快捷搜索内容
263+
selectionRows: [],//已选择的row
264+
filterFormData: null,//表单搜索数据
262265
tabsSelectdata: {},
263266
tabsActiveName: "all",
264267
};

resources/js/components/widgets/Base/Button.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
v-if="attrs.message"
88
>
99
<el-button
10+
:style="attrs.style"
11+
:class="attrs.className"
1012
slot="reference"
1113
:type="attrs.type"
1214
:size="attrs.size"
@@ -28,6 +30,8 @@
2830
v-else-if="attrs.tooltip"
2931
>
3032
<el-button
33+
:style="attrs.style"
34+
:class="attrs.className"
3135
:type="attrs.type"
3236
:size="attrs.size"
3337
:plain="attrs.plain"
@@ -44,6 +48,8 @@
4448
</el-tooltip>
4549
<el-button
4650
v-else
51+
:style="attrs.style"
52+
:class="attrs.className"
4753
:type="attrs.type"
4854
:size="attrs.size"
4955
:plain="attrs.plain"
@@ -59,6 +65,7 @@
5965
</el-button>
6066
<el-dialog
6167
v-if="attrs.dialog"
68+
:ref="attrs.dialog.ref || 'dialog'"
6269
:title="attrs.dialog.title"
6370
:visible.sync="dialogTableVisible"
6471
:width="attrs.dialog.width"
@@ -111,7 +118,10 @@ export default {
111118
}
112119
113120
if (this.attrs.refData) {
114-
this.$bus.emit(this.attrs.refData.ref, this.attrs.refData.data);
121+
this.$bus.emit(this.attrs.refData.ref, {
122+
data: this.attrs.refData.data,
123+
self: this,
124+
});
115125
return;
116126
}
117127

resources/js/mixins.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ const BaseComponent = {
3737
mounted() {
3838

3939
if (this.attrs && this.attrs.ref) {
40-
this.$bus.on(this.attrs.ref, (evalData) => {
41-
40+
this.$bus.on(this.attrs.ref, ({data,self}) => {
4241
let _this = this;
43-
new Function('ref',evalData)(_this)
42+
new Function('ref','self',data)(_this,self)
4443
})
4544
}
4645
},

src/Grid.php

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class Grid extends Component implements \JsonSerializable
6262
* @var string
6363
*/
6464
protected $dataUrl;
65+
66+
protected $customData;
67+
6568
protected $isGetData = false;
6669
private $actions;
6770
private $batchActions;
@@ -81,13 +84,17 @@ class Grid extends Component implements \JsonSerializable
8184
protected $dialogTitle = ['添加', '修改'];
8285

8386

84-
public function __construct(Eloquent $model)
87+
public function __construct(Eloquent $model = null)
8588
{
8689
$this->attributes = new Attributes();
8790
$this->dataUrl = request()->getUri();
8891
$this->model = new Model($model, $this);
89-
$this->keyName = $model->getKeyName();
90-
$this->defaultSort($model->getKeyName(), "asc");
92+
if ($model) {
93+
$this->keyName = $model->getKeyName();
94+
$this->defaultSort($model->getKeyName(), "asc");
95+
}
96+
97+
9198
$this->isGetData = request('get_data') == "true";
9299
$this->toolbars = new Toolbars($this);
93100
$this->batchActions = new BatchActions();
@@ -122,6 +129,15 @@ public function dataUrl(string $dataUrl)
122129
return $this;
123130
}
124131

132+
/**
133+
* @return bool
134+
*/
135+
public function isGetData(): bool
136+
{
137+
return $this->isGetData;
138+
}
139+
140+
125141
/**
126142
* 设置树形表格
127143
* @param bool $tree
@@ -174,17 +190,16 @@ protected function addColumn($name = '', $label = '', $columnKey = null)
174190
*/
175191
protected function addRelationColumn($name, $label = '')
176192
{
177-
list($relation, $column) = explode('.', $name);
193+
if ($this->model) {
194+
list($relation, $column) = explode('.', $name);
195+
$model = $this->model()->eloquent();
196+
if (!method_exists($model, $relation) || !$model->{$relation}() instanceof Relations\Relation) {
197+
} else {
198+
$this->model()->with($relation);
199+
}
178200

179-
$model = $this->model()->eloquent();
180-
181-
182-
if (!method_exists($model, $relation) || !$model->{$relation}() instanceof Relations\Relation) {
183-
} else {
184-
$this->model()->with($relation);
185201
}
186202

187-
188203
}
189204

190205
/**
@@ -295,12 +310,42 @@ public function bottom($closure)
295310
return $this;
296311
}
297312

313+
314+
/**
315+
* 自定义数据
316+
* @param $data
317+
* @param $current_page
318+
* @param $per_page
319+
* @param $last_page
320+
* @param $total
321+
* @return $this
322+
*/
323+
public function customData($data, $current_page, $per_page, $last_page, $total)
324+
{
325+
$this->customData = [
326+
'current_page' => (int)$current_page,
327+
'per_page' => (int)$per_page,
328+
'last_page' => (int)$last_page,
329+
'total' => (int)$total,
330+
'data' => $data,
331+
];
332+
return $this;
333+
}
334+
335+
298336
/**
299337
* data
300338
* @return array
301339
*/
302340
protected function data()
303341
{
342+
if ($this->customData) {
343+
$this->customData['data'] = $this->model()->displayData($this->customData['data']);
344+
return [
345+
'code' => 200,
346+
'data' => $this->customData
347+
];
348+
}
304349

305350
$this->applyQuery();
306351

@@ -345,6 +390,7 @@ public function jsonSerialize()
345390
$viewData['dialogForm'] = $this->dialogForm;
346391
$viewData['dialogFormWidth'] = $this->dialogFormWidth;
347392
$viewData['dialogTitle'] = $this->dialogTitle;
393+
$viewData['ref'] = $this->getRef();
348394
return $viewData;
349395
}
350396
}

src/Grid/Filter.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,25 @@ class Filter
5151

5252
protected static $supports = [
5353
'equal' => Filter\Equal::class,
54-
'notEqual' => Filter\NotEqual::class,
55-
'ilike' => Filter\Ilike::class,
54+
'notEqual' => Filter\NotEqual::class,
55+
'ilike' => Filter\Ilike::class,
5656
'like' => Filter\Like::class,
57-
'gt' => Filter\Gt::class,
58-
'lt' => Filter\Lt::class,
59-
'between' => Filter\Between::class,
60-
'where' => Filter\Where::class,
61-
'in' => Filter\In::class,
62-
'notIn' => Filter\NotIn::class,
63-
'date' => Filter\Date::class,
64-
'day' => Filter\Day::class,
65-
'month' => Filter\Month::class,
66-
'year' => Filter\Year::class,
67-
'contains' => Filter\Like::class,
57+
'gt' => Filter\Gt::class,
58+
'lt' => Filter\Lt::class,
59+
'between' => Filter\Between::class,
60+
'where' => Filter\Where::class,
61+
'in' => Filter\In::class,
62+
'notIn' => Filter\NotIn::class,
63+
'date' => Filter\Date::class,
64+
'day' => Filter\Day::class,
65+
'month' => Filter\Month::class,
66+
'year' => Filter\Year::class,
67+
'contains' => Filter\Like::class,
6868
'startsWith' => Filter\StartsWith::class,
69-
'endsWith' => Filter\EndsWith::class,
69+
'endsWith' => Filter\EndsWith::class,
7070
];
7171

72-
public function __construct(Model $model)
72+
public function __construct($model)
7373
{
7474
$this->model = $model;
7575

@@ -149,7 +149,6 @@ public function execute($toArray = true)
149149
);
150150

151151

152-
153152
return $this->model->addConditions($conditions)->buildData($toArray);
154153
}
155154

src/Grid/Model.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Model
103103
protected $eagerLoads = [];
104104

105105

106-
public function __construct(EloquentModel $model, Grid $grid = null)
106+
public function __construct(EloquentModel $model=null, Grid $grid = null)
107107
{
108108
$this->model = $model;
109109
$this->sModel = $model;
@@ -386,7 +386,7 @@ public function buildData($toArray = false)
386386
return $this->data;
387387
}
388388

389-
protected function displayData($data)
389+
public function displayData($data)
390390
{
391391
$columns = $this->grid->getColumns();
392392
$items = collect();
@@ -399,7 +399,7 @@ protected function displayData($data)
399399
if (Str::contains($column->getName(), '.')) {
400400
list($relationName, $relationColumn) = explode('.', $column->getName());
401401
//如果是集合
402-
if (data_get($row, $relationName) instanceof Collection) {
402+
if (data_get($row, $relationName) instanceof Collection || is_array(data_get($row, $relationName))) {
403403
$value = collect(data_get($row, $relationName))->pluck($relationColumn);
404404
$c_value = $column->customValueUsing($row, $value);
405405
data_set($item, $column->getName(), $c_value);

src/Grid/Toolbars.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Toolbars extends AdminJsonBuilder
2121

2222
protected $grid;
2323

24+
protected $show = true;
25+
2426
public function __construct(Grid $grid)
2527
{
2628

@@ -39,6 +41,17 @@ public function createButton(): CreateButton
3941
return $this->createButton;
4042
}
4143

44+
/**
45+
* 隐藏工具栏
46+
* @return Toolbars
47+
*/
48+
public function hide()
49+
{
50+
$this->show = false;
51+
return $this;
52+
}
53+
54+
4255

4356
/**
4457
* @param mixed $hideCreateButton
@@ -85,6 +98,7 @@ public function builderData()
8598
}
8699

87100
return [
101+
"show"=>$this->show,
88102
"left" => $left,
89103
"right" => $right
90104
];

0 commit comments

Comments
 (0)