Skip to content

Commit 6fc9a79

Browse files
committed
更新文档
1 parent e184ace commit 6fc9a79

File tree

3 files changed

+348
-70
lines changed

3 files changed

+348
-70
lines changed

docs/custom.md

Lines changed: 273 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,292 @@
1-
## 全局
2-
### Header右组件
3-
### Header左组件
4-
## 页面
5-
内容区域完全自定义
6-
## 表格
7-
内置表格自定义组件
8-
### 行操作
9-
数据行的操作
10-
#### 生成操作类
11-
暂无命令行创建功能,后面加入
12-
```php
13-
<?php
14-
namespace Admin\Actions;
15-
use SmallRuralDog\Admin\Actions\RowAction;
16-
class Delete extends RowAction
1+
# 自定义扩展
2+
3+
可以扩展属于自己业务逻辑的组件
4+
5+
## 创建扩展包
6+
7+
执行下面命令,生成一个扩展包
8+
9+
```bash
10+
php artisan admin:extend name
11+
#使用示例,以下会用这个名称来示例
12+
php artisan admin:extend smallruraldog/miss-meijiu-admin
13+
```
14+
15+
然后会在`Admin/Exends` 下创建一个扩展 `MissMeijiuAdmin`
16+
17+
进入到扩展根目录,就是`MissMeijiuAdmin`这个文件夹
18+
19+
检查你的项目`composer.json`配置文件,记住是项目的,不是扩展包的
20+
21+
```json
1722
{
18-
//操作k标识
19-
public $actionKey = "delete";
20-
//名称
21-
public $name = "删除";
22-
//确认提示,不设置则不提示
23-
public $confirm = "你确定要删除此条数据吗?";
24-
//后端处理请求类型
25-
public $httpMethod = "delete";
26-
//后端处理请求成功触发事件
27-
public $emit = "tableReload";
28-
29-
/**
30-
* Delete constructor.
31-
*/
32-
public function __construct()
33-
{
34-
//优先级1,前端路由跳转
35-
$this->vueRoute = $this->getRoutePath() . '/' . $this->getKey() . '/edit';
36-
//优先级2,后端请求处理
37-
$this->handleUrl = $this->getResource() . '/' . $this->getKey();
38-
//优先级3,url跳转
39-
$this->href = route('xxx');
23+
//....
24+
"require": {
25+
//...
26+
"smallruraldog/miss-meijiu-admin": "*"
27+
},
28+
//...
29+
"repositories": {
30+
//....
31+
"0": {
32+
"type": "path",
33+
//请确认好扩展包的路径,要不然回安装失败
34+
"url": "app/Admin/Extends/MissMeijiuAdmin"
35+
}
4036
}
4137
}
4238
```
43-
#### 使用Action
44-
```php
45-
$grid->addAction(new Delete());
46-
```
47-
### 批量操作
48-
表格批量操作,需要开启`selection`
49-
### 自定义按钮
5039

51-
## 表单
52-
自定义表单组件,如`编辑器``图片裁剪`
53-
### 创建扩展
54-
```bash
55-
php artisan admin:form-item {name}
40+
## 初始化扩展包
5641

57-
//例如
58-
php artisan admin:form-item smallruraldog/editor
42+
```bash
43+
composer update
5944
```
60-
### 初始化
61-
进入扩展根目录:扩展包在`admin-components`目录下
45+
46+
成功后执行
47+
6248
```bash
6349
npm install
6450
```
65-
### 开发编译
66-
```bash
51+
52+
成功后执行
53+
54+
```base
6755
npm run dev
56+
```
6857

69-
npm run watch-poll
58+
进入开发自动编译
59+
60+
```
61+
npm run watch
7062
```
7163

72-
### 开发教程
73-
可查看我写的一个[demo](https://github.com/SmallRuralDog/wangeditor),很简单的,一看就会,一写就成!!
64+
> 注意:如果失败,可能需要执行两次
65+
66+
## 目录结构说明
67+
68+
```
69+
- dist //vue编译后的文件目录
70+
- resource //前端文件目录
71+
- js
72+
- components //组件
73+
- extend.js //注册文件
74+
- sass
75+
- extend.scss //组件样式
76+
- src
77+
- components //组件定义类
78+
```
79+
80+
## 创建组件
81+
82+
### 表单字段组件
83+
84+
`src/components`下创建一个PHP类
7485

75-
### 使用扩展
7686
```php
77-
$form->item('content', '内容')->displayComponent(Editor::make())
87+
namespace Smallruraldog\MissMeijiuAdmin\Components;
88+
89+
use SmallRuralDog\Admin\Components\Component;
90+
//需要继承 Component
91+
class GoodsSku extends Component
92+
{
93+
//组件的名称,等下注册vue组件的时候名称需要一致
94+
protected $componentName = "GoodsSku";
95+
96+
/** 示例属性 *******************************/
97+
98+
//当前所有规格
99+
protected $goodsAttrs = [];
100+
//添加规格接口
101+
protected $addGoodsAttrUrl;
102+
protected $addGoodsAttrValueUrl;
103+
protected $uploadComponent;
104+
protected $imageComponent;
105+
/*********************************/
106+
107+
//需要隐藏的属性,设置后不会在json中输出
108+
public $hideAttrs = [];
109+
110+
public function __construct($value = [])
111+
{
112+
//字段初始值
113+
$this->componentValue($this->getValue([]));
114+
115+
/** 示例代码 *******************************/
116+
$goodsAttrModel = new GoodsAttr();
117+
$this->goodsAttrs = $goodsAttrModel->allAttrs();
118+
$this->addGoodsAttrUrl = route("addGoodsAttr");
119+
$this->addGoodsAttrValueUrl = route("addGoodsAttrValue");
120+
$this->uploadComponent = Upload::make()->width(130)->height(130);
121+
$this->imageComponent = Image::make()->size(30, 30)->className("mr-10");
122+
/*********************************/
123+
}
124+
//定义一个make
125+
public static function make($value = [])
126+
{
127+
return new GoodsSku($value);
128+
}
129+
130+
//自定义字段输出值,在表单编辑时使用
131+
public function getValue($data)
132+
{
133+
return [
134+
'goods_attrs' => [],
135+
'goods_sku_list' => []
136+
];
137+
}
138+
}
78139
```
79140

80-
### 编译
81-
```bash
82-
npm run production
141+
`resources\js\components`下创建vue文件,例如`GoodsSku.vue`
142+
143+
```vue
144+
<template>
145+
<!--这是属性示例用法-->
146+
<el-input
147+
:style="attrs.style"
148+
:class="attrs.className"
149+
:validate-event="attrs.validateEvent"
150+
:value="value" <!--绑定字段的值-->
151+
@input="onChange" <!--改变字段的值-->
152+
>
153+
</el-input>
154+
</template>
155+
<script>
156+
export default {
157+
props: {
158+
//组件类定义的属性
159+
attrs: Object,
160+
//当前表单的所有字段的值
161+
form_data: Object,
162+
//当前字段的值,注意:不一定的null
163+
value: {
164+
default: null
165+
}
166+
},
167+
data() {
168+
return {
169+
};
170+
},
171+
//上层实现了v-model功能
172+
model: {
173+
prop: "value",
174+
event: "change"
175+
},
176+
methods: {
177+
//改变字段的值
178+
onChange(value) {
179+
this.$emit("change", value);
180+
}
181+
}
182+
};
183+
</script>
184+
```
185+
186+
然后在`extend.js`中注册组件
187+
188+
```js
189+
VueAdmin.booting((Vue, router, store) => {
190+
//....................
191+
Vue.component("GoodsSku", require('./components/GoodsSku').default),
192+
});
193+
```
194+
195+
在代码中使用组件
196+
197+
```php
198+
$form->item("goods_sku", "产品规格")->displayComponent(GoodsSku::make())
199+
```
200+
201+
### 表格字段组件
202+
203+
### 表格操作组件
204+
205+
### 表格工具栏组件
206+
207+
### Page组件
208+
209+
如果内置的组件无法满足你的需求,那么就需要自定义了
210+
211+
创建组件定义类
212+
213+
```php
214+
class MyPage extends Component
215+
{
216+
protected $componentName = 'MyPage';
217+
218+
public static function make()
219+
{
220+
return new MyPage();
221+
}
222+
}
223+
```
224+
225+
创建组件vue文件
226+
227+
```vue
228+
<template>
229+
230+
</template>
231+
<script>
232+
export default {
233+
props: {
234+
attrs: Object
235+
},
236+
};
237+
</script>
238+
<style lang="scss"></style>
83239
```
84-
### 发布到Composer
85-
请自行搜索相关教程,欢迎PR
240+
241+
调用组件
242+
243+
```php
244+
public function index(Content $content)
245+
{
246+
$content->body(MyPage::make());
247+
return $content;
248+
}
249+
```
250+
251+
252+
253+
## 组件通信
254+
255+
由于各个组件需要通信,所以需要监听或触发各种事件
256+
257+
```js
258+
//监听事件
259+
this.$bus.on("EventName",()=>{
260+
})
261+
//触发事件
262+
this.$bus.emit("EventName")
263+
//注销监听
264+
this.$bus.off("EventName")
265+
```
266+
267+
268+
269+
### 编辑数据加载完毕
270+
271+
由于编辑数据是异步加载的,在组件初始化的时候拿不到真正的数据
272+
273+
```js
274+
mounted() {
275+
this.$bus.on("EditDataLoadingCompleted", () => {
276+
277+
});
278+
},
279+
destroyed() {
280+
try {
281+
this.$bus.off("EditDataLoadingCompleted");
282+
} catch (e) {}
283+
},
284+
```
285+
286+
### 刷新表格
287+
288+
```js
289+
this.$bus.emit("tableReload")
290+
```
291+
292+
更多事件正在开发中......

0 commit comments

Comments
 (0)