Skip to content

Commit 70ba261

Browse files
committed
更新 自定义组件文档
1 parent 5ed23eb commit 70ba261

File tree

11 files changed

+671
-55
lines changed

11 files changed

+671
-55
lines changed
14.7 KB
Loading

docs/custom.md

Lines changed: 298 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ npm run production
8787

8888
## 创建组件
8989

90-
### 表单字段组件
90+
### Form字段组件
9191

9292
`src/components`下创建一个PHP类
9393

@@ -208,11 +208,305 @@ VueAdmin.booting((Vue, router, store) => {
208208
$form->item("goods_sku", "产品规格")->displayComponent(GoodsSku::make())
209209
```
210210

211-
### 表格字段组件
211+
### Grid字段组件
212+
213+
### Grid操作组件
214+
215+
自定义表格行操作
216+
217+
> 下面以ActionButton为例
218+
219+
创建一个组件类 继承 `BaseRowAction`
220+
221+
```php
222+
class ActionButton extends BaseRowAction
223+
{
224+
use Button;//导入Button属性
225+
226+
protected $uri;
227+
protected $componentName = "ActionButton";
228+
protected $handler;
229+
230+
public function __construct($content)
231+
{
232+
$this->content = $content;
233+
$this->type("text");
234+
}
235+
236+
/**
237+
* @param string $content 按钮内容
238+
* @return ToolButton
239+
*/
240+
public static function make($content)
241+
{
242+
return new ActionButton($content);
243+
}
244+
245+
/**
246+
* @param mixed $uri
247+
* @return $this
248+
*/
249+
public function uri($uri)
250+
{
251+
$this->uri = $uri;
252+
return $this;
253+
}
254+
255+
/**
256+
* @param string $handler 响应类型 request|route|link
257+
* @return $this
258+
*/
259+
public function handler($handler)
260+
{
261+
$this->handler = $handler;
262+
return $this;
263+
}
264+
}
265+
```
266+
267+
创建组件Vue文件
268+
269+
```vue
270+
<template>
271+
<el-button
272+
:type="action.type"
273+
:size="action.size"
274+
:plain="action.plain"
275+
:round="action.round"
276+
:circle="action.circle"
277+
:disabled="action.disabled"
278+
:icon="action.icon"
279+
:autofocus="action.autofocus"
280+
:loading="loading"
281+
class="mr-10"
282+
@click="onClick"
283+
>{{ action.content }}</el-button
284+
>
285+
</template>
286+
<script>
287+
export default {
288+
props: {
289+
scope: Object,//当前行的字段定义和数据
290+
action: Object,//当前主键的属性
291+
key_name: String//主键名称
292+
},
293+
data() {
294+
return {
295+
loading: false
296+
};
297+
},
298+
methods: {
299+
onClick() {
300+
//判断操作响应类型
301+
switch (this.action.handler) {
302+
case "route":
303+
this.$route.push(this.uri);
304+
break;
305+
case "link":
306+
window.location.href = this.uri;
307+
break;
308+
case "request":
309+
this.onRequest(this.uri);
310+
break;
311+
default:
312+
this.$Message.warning("响应类型未定义");
313+
break;
314+
}
315+
},
316+
onRequest(uri) {
317+
this.loading = true;
318+
this.$http
319+
.get(uri)
320+
.then(res => {
321+
if (res.code == 200) {
322+
}
323+
})
324+
.finally(() => {
325+
this.loading = false;
326+
});
327+
}
328+
},
329+
computed: {
330+
uri() {
331+
//替换变量
332+
let uri = this.action.uri;
333+
this._.forEach(this.row, (value, key) => {
334+
uri = this._.replace(uri, "{" + key + "}", value);
335+
});
336+
return uri;
337+
},
338+
//当前表格/树形表格的字段定义
339+
colum() {
340+
return this.scope.colum;
341+
},
342+
//当前行的值
343+
row() {
344+
return this.scope.row;
345+
},
346+
//主键值
347+
key() {
348+
return this.scope.row[this.key_name];
349+
}
350+
}
351+
};
352+
</script>
353+
```
354+
355+
注册Vue组件
356+
357+
```js
358+
Vue.component('ActionButton', require('xxx').default);
359+
```
360+
361+
使用自定义组件
362+
363+
```php
364+
$grid->actions(function (Grid\Actions $actions) {
365+
$actions->add(Grid\Actions\ActionButton::make("公众号管理")->order(3)->handler('route')->uri("WeChat/manage/{app_id}"));
366+
});
367+
```
368+
369+
370+
371+
### Grid工具栏组件
372+
373+
![image-20200313100328876](custom.assets/image-20200313100328876.png)
374+
375+
下面以ToolButton为例
376+
377+
按照国际惯例,创建组件类 继承`BaseAction`
378+
379+
```php
380+
class ToolButton extends BaseAction
381+
{
382+
use Button;
383+
protected $uri;
384+
protected $componentName = "ToolButton";
385+
protected $handler;
386+
387+
public function __construct($content)
388+
{
389+
$this->content = $content;
390+
}
391+
392+
/**
393+
* @param string $content 按钮内容
394+
* @return ToolButton
395+
*/
396+
public static function make($content)
397+
{
398+
return new ToolButton($content);
399+
}
400+
401+
/**
402+
* @param mixed $uri
403+
* @return $this
404+
*/
405+
public function uri($uri)
406+
{
407+
$this->uri = $uri;
408+
return $this;
409+
}
410+
411+
/**
412+
* @param string $handler 响应类型 request|route|link
413+
* @return $this
414+
*/
415+
public function handler($handler)
416+
{
417+
$this->handler = $handler;
418+
return $this;
419+
}
420+
421+
422+
}
423+
```
424+
425+
创建vue文件
426+
427+
```php
428+
<template>
429+
<el-button
430+
:type="attrs.type"
431+
:size="attrs.size"
432+
:plain="attrs.plain"
433+
:round="attrs.round"
434+
:circle="attrs.circle"
435+
:disabled="attrs.disabled"
436+
:icon="attrs.icon"
437+
:autofocus="attrs.autofocus"
438+
:loading="loading"
439+
class="mr-10"
440+
@click="onClick"
441+
>{{ attrs.content }}</el-button
442+
>
443+
</template>
444+
<script>
445+
export default {
446+
props: {
447+
//组件属性
448+
attrs: Object
449+
},
450+
data() {
451+
return {
452+
loading: false
453+
};
454+
},
455+
methods: {
456+
onClick() {
457+
switch (this.attrs.handler) {
458+
case "route":
459+
this.$router.push(this.attrs.uri);
460+
break;
461+
case "link":
462+
window.location.href = this.attrs.uri;
463+
break;
464+
case "request":
465+
this.onRequest(this.attrs.uri);
466+
break;
467+
default:
468+
this.$Message.warning("响应类型未定义");
469+
break;
470+
}
471+
},
472+
onRequest(uri) {
473+
this.loading = true;
474+
this.$http
475+
.get(uri)
476+
.then(res => {
477+
if (res.code == 200) {
478+
479+
}
480+
})
481+
.finally(() => {
482+
this.loading = false;
483+
});
484+
}
485+
}
486+
};
487+
</script>
488+
```
489+
490+
注册Vue组件
491+
492+
```js
493+
Vue.component('ToolButton', require('xxx').default);
494+
```
495+
496+
调用组件
497+
498+
```php
499+
$fansListGrid->toolbars(function (Grid\Toolbars $toolbars) use ($app_id) {
500+
$toolbars->hideCreateButton();
501+
$toolbars->addRight(Grid\Tools\ToolButton::make("同步粉丝")
502+
->icon("el-icon-refresh")
503+
->type("primary")
504+
->handler("request")
505+
->uri(route("WeChat/syncFans", ['app_id' => $app_id])));
506+
});
507+
```
212508

213-
### 表格操作组件
214509

215-
### 表格工具栏组件
216510

217511
### Page组件
218512

docs/grid.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -666,27 +666,29 @@ $actions->hideActions()
666666

667667
#### 隐藏详情操作
668668

669+
当前版本暂无详情功能
670+
669671
```php
670672
$actions->hideViewAction()
671673
```
672674

673675
#### 隐藏编辑操作
674676

675-
```
677+
```php
676678
$actions->hideEditAction()
677679
```
678680

679681
#### 隐藏删除操作
680682

681-
```
683+
```php
682684
$actions->hideDeleteAction()
683685
```
684686

685687
#### 添加自定义操作
686688

687-
创建自定义操作请查看 [如何创建自定义操作]()
689+
创建自定义操作请查看 [如何创建自定义操作](https://smallruraldog.github.io/laravel-vue-admin/#/custom?id=%e8%a1%a8%e6%a0%bc%e6%93%8d%e4%bd%9c%e7%bb%84%e4%bb%b6)
688690

689-
```
691+
```php
690692
$actions->add(new MyAction())
691693
```
692694

@@ -708,7 +710,7 @@ $toolbars->hideCreateButton()
708710

709711
#### 添加自定义工具
710712

711-
详情请看[如何创建自定义工具]()
713+
详情请看[如何创建自定义工具](https://smallruraldog.github.io/laravel-vue-admin/#/custom?id=%e8%a1%a8%e6%a0%bc%e5%b7%a5%e5%85%b7%e6%a0%8f%e7%bb%84%e4%bb%b6)
712714

713715
```php
714716
$toolbars->addLeft(new MyLeftTool());//添加在左侧

0 commit comments

Comments
 (0)