ylwl-cpscoms 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/DtTable/index.vue.js +379 -0
- package/es/DtTable/index.vue2.js +719 -0
- package/es/DtTable/index.vue3.js +6 -0
- package/es/SlAlert/index.vue.js +1 -1
- package/es/SlAlert/index.vue3.js +1 -1
- package/es/SlForm/index.vue.js +1 -1
- package/es/SlForm/index.vue3.js +1 -1
- package/es/SlPage/index.vue.js +2 -2
- package/es/SlPage/index.vue3.js +1 -1
- package/es/index.js +3 -1
- package/package.json +1 -1
- package/src/DtTable/DtTable/347/273/204/344/273/266/344/275/277/347/224/250/346/226/207/346/241/243.md +819 -0
- package/src/DtTable/index.vue +779 -0
- package/src/SlAlert/index.vue +2 -1
- package/src/SlForm/index.vue +142 -1
- package/src/SlPage/index.vue +39 -2
- package/src/index.js +3 -1
- package/src/SlForm/index.css +0 -141
- package/src/SlPage/index.css +0 -38
|
@@ -0,0 +1,819 @@
|
|
|
1
|
+
# DtTable 组件使用文档
|
|
2
|
+
|
|
3
|
+
> 基于 Element UI 封装的可编辑表格组件,支持多种编辑器类型、表单验证、异步数据加载、联动等功能。
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 目录
|
|
8
|
+
|
|
9
|
+
- [基础用法](#基础用法)
|
|
10
|
+
- [Props 属性](#props-属性)
|
|
11
|
+
- [gridOptions 配置](#gridoptions-配置)
|
|
12
|
+
- [columns 列配置](#columns-列配置)
|
|
13
|
+
- [编辑器类型](#编辑器类型)
|
|
14
|
+
- [操作按钮配置](#操作按钮配置)
|
|
15
|
+
- [Methods 方法](#methods-方法)
|
|
16
|
+
- [Events 事件](#events-事件)
|
|
17
|
+
- [高级功能](#高级功能)
|
|
18
|
+
- [完整示例](#完整示例)
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 基础用法
|
|
23
|
+
|
|
24
|
+
```vue
|
|
25
|
+
<template>
|
|
26
|
+
<DtTable ref="dtTable" :grid-options="gridOptions" />
|
|
27
|
+
</template>
|
|
28
|
+
|
|
29
|
+
<script>
|
|
30
|
+
import DtTable from '@/components/global/DtTable'
|
|
31
|
+
|
|
32
|
+
export default {
|
|
33
|
+
components: { DtTable },
|
|
34
|
+
data() {
|
|
35
|
+
return {
|
|
36
|
+
gridOptions: {
|
|
37
|
+
data: [
|
|
38
|
+
{ name: '张三', age: 25, city: '北京' },
|
|
39
|
+
{ name: '李四', age: 30, city: '上海' }
|
|
40
|
+
],
|
|
41
|
+
columns: [
|
|
42
|
+
{ field: 'name', title: '姓名' },
|
|
43
|
+
{ field: 'age', title: '年龄' },
|
|
44
|
+
{ field: 'city', title: '城市' }
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
</script>
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## Props 属性
|
|
56
|
+
|
|
57
|
+
| 属性名 | 类型 | 默认值 | 必填 | 说明 |
|
|
58
|
+
|--------|------|--------|------|------|
|
|
59
|
+
| gridOptions | Object | - | 是 | 表格配置对象,详见 [gridOptions 配置](#gridoptions-配置) |
|
|
60
|
+
| isDisabled | Boolean | false | 否 | 全局禁用所有编辑器 |
|
|
61
|
+
| pagination | Object | 见下方 | 否 | 分页配置 |
|
|
62
|
+
| apiFunction | Function | - | 否 | 数据请求 API 函数,传入后自动启用分页 |
|
|
63
|
+
| apiParams | Object | - | 否 | API 请求额外参数 |
|
|
64
|
+
| immediate | Boolean | false | 否 | 是否在 mounted 时立即加载数据 |
|
|
65
|
+
|
|
66
|
+
### pagination 默认值
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
{
|
|
70
|
+
currentPage: 1,
|
|
71
|
+
pageSize: 10,
|
|
72
|
+
total: 0,
|
|
73
|
+
pageSizes: [10, 20, 50, 100],
|
|
74
|
+
layout: 'total, sizes, prev, pager, next, jumper',
|
|
75
|
+
background: true,
|
|
76
|
+
small: false,
|
|
77
|
+
disabled: false,
|
|
78
|
+
hideOnSinglePage: false
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## gridOptions 配置
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
gridOptions: {
|
|
88
|
+
// 表格数据
|
|
89
|
+
data: [],
|
|
90
|
+
|
|
91
|
+
// 列配置
|
|
92
|
+
columns: [],
|
|
93
|
+
|
|
94
|
+
// 操作列按钮
|
|
95
|
+
actions: [],
|
|
96
|
+
|
|
97
|
+
// 滚动区域宽高
|
|
98
|
+
scroll: {
|
|
99
|
+
x: '1200px', // 横向滚动宽度
|
|
100
|
+
y: 400 // 纵向滚动高度(默认400)
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
// 操作列配置
|
|
104
|
+
actionWidth: '100px', // 操作列宽度
|
|
105
|
+
actionAlign: 'left', // 操作列对齐方式
|
|
106
|
+
actionFixed: 'right' // 操作列固定位置
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## columns 列配置
|
|
113
|
+
|
|
114
|
+
### 基础属性
|
|
115
|
+
|
|
116
|
+
| 属性名 | 类型 | 说明 |
|
|
117
|
+
|--------|------|------|
|
|
118
|
+
| field | String | 字段名,对应数据中的 key |
|
|
119
|
+
| title | String | 列标题 |
|
|
120
|
+
| hidden | Boolean | 是否隐藏该列 |
|
|
121
|
+
| attrs | Object | 传递给 `el-table-column` 的额外属性(如 width, minWidth, fixed 等) |
|
|
122
|
+
|
|
123
|
+
### 示例
|
|
124
|
+
|
|
125
|
+
```javascript
|
|
126
|
+
columns: [
|
|
127
|
+
{
|
|
128
|
+
field: 'name',
|
|
129
|
+
title: '姓名',
|
|
130
|
+
attrs: { width: 120, fixed: 'left' }
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
field: 'age',
|
|
134
|
+
title: '年龄',
|
|
135
|
+
attrs: { minWidth: 80, align: 'center' }
|
|
136
|
+
}
|
|
137
|
+
]
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## 编辑器类型
|
|
143
|
+
|
|
144
|
+
通过 `editor` 属性指定列的编辑器类型。
|
|
145
|
+
|
|
146
|
+
### 1. 文本显示(默认)
|
|
147
|
+
|
|
148
|
+
不设置 `editor` 或不配置,直接显示文本。
|
|
149
|
+
|
|
150
|
+
```javascript
|
|
151
|
+
{
|
|
152
|
+
field: 'name',
|
|
153
|
+
title: '姓名'
|
|
154
|
+
// 显示为文本,空值显示 "-"
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### 2. 自定义渲染 (custom)
|
|
159
|
+
|
|
160
|
+
通过 `render` 函数自定义渲染内容,支持 h 函数。
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
{
|
|
164
|
+
field: 'status',
|
|
165
|
+
title: '状态',
|
|
166
|
+
editor: 'custom',
|
|
167
|
+
render: ({ row, h }) => {
|
|
168
|
+
// 返回文本
|
|
169
|
+
return row.status === 1 ? '启用' : '禁用'
|
|
170
|
+
|
|
171
|
+
// 返回 VNode(使用 h 函数)
|
|
172
|
+
return h('el-tag', {
|
|
173
|
+
props: { type: row.status === 1 ? 'success' : 'danger' }
|
|
174
|
+
}, row.status === 1 ? '启用' : '禁用')
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### 3. 选择器 (select)
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
{
|
|
183
|
+
field: 'city',
|
|
184
|
+
title: '城市',
|
|
185
|
+
editor: 'select',
|
|
186
|
+
placeholder: '请选择城市',
|
|
187
|
+
options: [
|
|
188
|
+
{ label: '北京', value: 'beijing' },
|
|
189
|
+
{ label: '上海', value: 'shanghai' }
|
|
190
|
+
],
|
|
191
|
+
// 自定义选项字段名
|
|
192
|
+
labelKey: 'name', // 默认 'label'
|
|
193
|
+
valueKey: 'code', // 默认 'value'
|
|
194
|
+
// 是否可搜索
|
|
195
|
+
filterable: true,
|
|
196
|
+
// 是否远程搜索
|
|
197
|
+
remote: false,
|
|
198
|
+
// 事件
|
|
199
|
+
events: {
|
|
200
|
+
change(value, row, index) {
|
|
201
|
+
console.log('选择变化:', value)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### 4. 输入框 (input)
|
|
208
|
+
|
|
209
|
+
```javascript
|
|
210
|
+
{
|
|
211
|
+
field: 'name',
|
|
212
|
+
title: '姓名',
|
|
213
|
+
editor: 'input',
|
|
214
|
+
placeholder: '请输入姓名',
|
|
215
|
+
events: {
|
|
216
|
+
change(value, row, index) {
|
|
217
|
+
console.log('输入变化:', value)
|
|
218
|
+
},
|
|
219
|
+
focus(event, row) {
|
|
220
|
+
console.log('获取焦点')
|
|
221
|
+
},
|
|
222
|
+
blur(event, row, index) {
|
|
223
|
+
console.log('失去焦点')
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### 5. 数字输入框 (input-number)
|
|
230
|
+
|
|
231
|
+
```javascript
|
|
232
|
+
{
|
|
233
|
+
field: 'quantity',
|
|
234
|
+
title: '数量',
|
|
235
|
+
editor: 'input-number',
|
|
236
|
+
min: 1, // 最小值,支持函数 (row, index) => number
|
|
237
|
+
max: 100, // 最大值,支持函数 (row, index) => number
|
|
238
|
+
step: 1, // 步长
|
|
239
|
+
precision: 0, // 小数精度
|
|
240
|
+
controlsPosition: '', // 控制按钮位置 ('right')
|
|
241
|
+
unit: '个', // 单位后缀,支持字符串或 h 函数
|
|
242
|
+
attrs: { width: 180 }
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
**unit 支持 h 函数**:
|
|
247
|
+
|
|
248
|
+
```javascript
|
|
249
|
+
{
|
|
250
|
+
field: 'price',
|
|
251
|
+
title: '价格',
|
|
252
|
+
editor: 'input-number',
|
|
253
|
+
unit: (h, scope) => {
|
|
254
|
+
return h('span', { style: { color: '#f56c6c' } }, '元')
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### 6. 开关 (switch)
|
|
260
|
+
|
|
261
|
+
```javascript
|
|
262
|
+
{
|
|
263
|
+
field: 'enable',
|
|
264
|
+
title: '是否启用',
|
|
265
|
+
editor: 'switch',
|
|
266
|
+
events: {
|
|
267
|
+
change(value, row, index) {
|
|
268
|
+
console.log('开关变化:', value)
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### 7. 时间选择器 (time)
|
|
275
|
+
|
|
276
|
+
```javascript
|
|
277
|
+
{
|
|
278
|
+
field: 'time',
|
|
279
|
+
title: '时间',
|
|
280
|
+
editor: 'time',
|
|
281
|
+
placeholder: '选择时间',
|
|
282
|
+
attrs: {
|
|
283
|
+
'value-format': 'HH:mm:ss',
|
|
284
|
+
'picker-options': { selectableRange: '00:00:00 - 23:59:59' }
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### 8. 日期选择器 (date)
|
|
290
|
+
|
|
291
|
+
```javascript
|
|
292
|
+
{
|
|
293
|
+
field: 'date',
|
|
294
|
+
title: '日期',
|
|
295
|
+
editor: 'date',
|
|
296
|
+
placeholder: '选择日期',
|
|
297
|
+
attrs: {
|
|
298
|
+
'value-format': 'yyyy-MM-dd',
|
|
299
|
+
type: 'date'
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### 9. 日期时间选择器 (datetime)
|
|
305
|
+
|
|
306
|
+
```javascript
|
|
307
|
+
{
|
|
308
|
+
field: 'datetime',
|
|
309
|
+
title: '日期时间',
|
|
310
|
+
editor: 'datetime',
|
|
311
|
+
placeholder: '选择日期时间',
|
|
312
|
+
attrs: {
|
|
313
|
+
'value-format': 'yyyy-MM-dd HH:mm:ss'
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### 10. 日期范围选择器 (daterange)
|
|
319
|
+
|
|
320
|
+
```javascript
|
|
321
|
+
{
|
|
322
|
+
field: 'dateRange',
|
|
323
|
+
title: '日期范围',
|
|
324
|
+
editor: 'daterange',
|
|
325
|
+
placeholder: '选择日期范围',
|
|
326
|
+
attrs: {
|
|
327
|
+
'value-format': 'yyyy-MM-dd'
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
---
|
|
333
|
+
|
|
334
|
+
### 公共编辑器属性
|
|
335
|
+
|
|
336
|
+
以下属性适用于所有编辑器类型:
|
|
337
|
+
|
|
338
|
+
| 属性名 | 类型 | 说明 |
|
|
339
|
+
|--------|------|------|
|
|
340
|
+
| placeholder | String | 占位符文本 |
|
|
341
|
+
| disabled | Boolean / Function(row, index) | 是否禁用 |
|
|
342
|
+
| rules | Array | 表单验证规则 |
|
|
343
|
+
| events | Object | 事件处理 { change, focus, blur, clear } |
|
|
344
|
+
|
|
345
|
+
### 禁用配置
|
|
346
|
+
|
|
347
|
+
```javascript
|
|
348
|
+
// 固定禁用
|
|
349
|
+
{ field: 'name', disabled: true }
|
|
350
|
+
|
|
351
|
+
// 动态禁用(函数)
|
|
352
|
+
{
|
|
353
|
+
field: 'name',
|
|
354
|
+
disabled: (row, index) => {
|
|
355
|
+
return row.type === 1 // type 为 1 时禁用
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
---
|
|
361
|
+
|
|
362
|
+
## 操作按钮配置
|
|
363
|
+
|
|
364
|
+
```javascript
|
|
365
|
+
gridOptions: {
|
|
366
|
+
actions: [
|
|
367
|
+
{
|
|
368
|
+
text: '编辑', // 按钮文字
|
|
369
|
+
type: 'primary', // 按钮类型 (primary / success / warning / danger / info / text)
|
|
370
|
+
icon: 'el-icon-edit', // 图标
|
|
371
|
+
style: { color: '#409EFF' }, // 自定义样式
|
|
372
|
+
show: true, // 是否显示,支持 Boolean 或 Function(row, index)
|
|
373
|
+
disabled: false, // 是否禁用,支持 Boolean 或 Function(row)
|
|
374
|
+
click(row, index) { // 点击事件
|
|
375
|
+
console.log('编辑:', row, index)
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
{
|
|
379
|
+
text: '删除',
|
|
380
|
+
type: 'text',
|
|
381
|
+
style: { color: '#F56C6C' },
|
|
382
|
+
show: (row, index) => row.status !== 1, // 状态为 1 时隐藏
|
|
383
|
+
disabled: (row) => row.locked, // 锁定时禁用
|
|
384
|
+
click(row, index) {
|
|
385
|
+
console.log('删除:', row, index)
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
],
|
|
389
|
+
actionWidth: '150px', // 操作列宽度
|
|
390
|
+
actionAlign: 'center', // 对齐方式
|
|
391
|
+
actionFixed: 'right' // 固定在右侧
|
|
392
|
+
}
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
---
|
|
396
|
+
|
|
397
|
+
## Methods 方法
|
|
398
|
+
|
|
399
|
+
通过 `ref` 调用组件方法:
|
|
400
|
+
|
|
401
|
+
```javascript
|
|
402
|
+
this.$refs.dtTable.validate()
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### 表格操作
|
|
406
|
+
|
|
407
|
+
| 方法名 | 参数 | 返回值 | 说明 |
|
|
408
|
+
|--------|------|--------|------|
|
|
409
|
+
| getSelection() | - | Array | 获取当前选中的行数据 |
|
|
410
|
+
| clearSelection() | - | void | 清空所有选中状态 |
|
|
411
|
+
| toggleRowSelection(row, selected) | row: Object, selected: Boolean | void | 切换某行的选中状态 |
|
|
412
|
+
| scrollToBottom() | - | void | 滚动表格到底部 |
|
|
413
|
+
|
|
414
|
+
### 数据请求
|
|
415
|
+
|
|
416
|
+
| 方法名 | 参数 | 返回值 | 说明 |
|
|
417
|
+
|--------|------|--------|------|
|
|
418
|
+
| refresh(params) | params: Object | Promise | 刷新表格数据,可传入额外参数 |
|
|
419
|
+
|
|
420
|
+
```javascript
|
|
421
|
+
// 刷新数据
|
|
422
|
+
this.$refs.dtTable.refresh()
|
|
423
|
+
|
|
424
|
+
// 带参数刷新
|
|
425
|
+
this.$refs.dtTable.refresh({ keyword: '搜索词' })
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
### 表单验证
|
|
429
|
+
|
|
430
|
+
| 方法名 | 参数 | 返回值 | 说明 |
|
|
431
|
+
|--------|------|--------|------|
|
|
432
|
+
| validate() | - | Promise | 验证所有表单字段 |
|
|
433
|
+
| resetFields() | - | void | 重置所有表单字段 |
|
|
434
|
+
| clearValidate() | - | void | 清除所有验证状态 |
|
|
435
|
+
|
|
436
|
+
```javascript
|
|
437
|
+
// 验证表单
|
|
438
|
+
try {
|
|
439
|
+
await this.$refs.dtTable.validate()
|
|
440
|
+
console.log('验证通过')
|
|
441
|
+
} catch (error) {
|
|
442
|
+
console.log('验证失败')
|
|
443
|
+
}
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
### 联动操作
|
|
447
|
+
|
|
448
|
+
| 方法名 | 参数 | 返回值 | 说明 |
|
|
449
|
+
|--------|------|--------|------|
|
|
450
|
+
| triggerLinkage(field, type) | field: String, type: String | void | 手动触发指定字段的联动 |
|
|
451
|
+
|
|
452
|
+
```javascript
|
|
453
|
+
// 手动触发联动
|
|
454
|
+
this.$refs.dtTable.triggerLinkage('province', 'init')
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
---
|
|
458
|
+
|
|
459
|
+
## Events 事件
|
|
460
|
+
|
|
461
|
+
| 事件名 | 回调参数 | 说明 |
|
|
462
|
+
|--------|----------|------|
|
|
463
|
+
| page-change | page: Number | 页码变化时触发 |
|
|
464
|
+
| size-change | size: Number | 每页条数变化时触发 |
|
|
465
|
+
| update:pagination | pagination: Object | 分页信息更新时触发 |
|
|
466
|
+
| action-click | button, row, index | 操作按钮点击时触发 |
|
|
467
|
+
| validate-error | error | 表单验证失败时触发 |
|
|
468
|
+
| selection-change | selection: Array | 选择项变化时触发 |
|
|
469
|
+
| async-load | { field, row, index, options } | 异步选项加载完成时触发 |
|
|
470
|
+
| linkage-change | { sourceField, targetField, value, row, index, options } | 联动数据变化时触发 |
|
|
471
|
+
|
|
472
|
+
### 使用示例
|
|
473
|
+
|
|
474
|
+
```vue
|
|
475
|
+
<DtTable
|
|
476
|
+
:grid-options="gridOptions"
|
|
477
|
+
@action-click="handleActionClick"
|
|
478
|
+
@selection-change="handleSelectionChange"
|
|
479
|
+
@page-change="handlePageChange"
|
|
480
|
+
/>
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
```javascript
|
|
484
|
+
methods: {
|
|
485
|
+
handleActionClick(button, row, index) {
|
|
486
|
+
console.log('点击了按钮:', button.text)
|
|
487
|
+
console.log('行数据:', row)
|
|
488
|
+
console.log('行索引:', index)
|
|
489
|
+
},
|
|
490
|
+
handleSelectionChange(selection) {
|
|
491
|
+
console.log('选中的数据:', selection)
|
|
492
|
+
},
|
|
493
|
+
handlePageChange(page) {
|
|
494
|
+
console.log('当前页:', page)
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
---
|
|
500
|
+
|
|
501
|
+
## 高级功能
|
|
502
|
+
|
|
503
|
+
### 1. 多选列
|
|
504
|
+
|
|
505
|
+
在 `columns` 中添加 `field: 'selection'` 即可启用多选:
|
|
506
|
+
|
|
507
|
+
```javascript
|
|
508
|
+
columns: [
|
|
509
|
+
{ field: 'selection', title: '' },
|
|
510
|
+
{ field: 'name', title: '姓名' },
|
|
511
|
+
// ...
|
|
512
|
+
]
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
### 2. 异步加载选项
|
|
516
|
+
|
|
517
|
+
适用于 select 编辑器,动态加载选项数据:
|
|
518
|
+
|
|
519
|
+
```javascript
|
|
520
|
+
{
|
|
521
|
+
field: 'city',
|
|
522
|
+
title: '城市',
|
|
523
|
+
editor: 'select',
|
|
524
|
+
asyncOptions: {
|
|
525
|
+
immediate: true, // 是否立即加载
|
|
526
|
+
async loadData(row) {
|
|
527
|
+
// row 为当前行数据,可据此加载不同选项
|
|
528
|
+
const { data } = await api.getCityList({ provinceId: row.provinceId })
|
|
529
|
+
return data.map(item => ({
|
|
530
|
+
label: item.cityName,
|
|
531
|
+
value: item.cityId
|
|
532
|
+
}))
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
### 3. 联动配置
|
|
539
|
+
|
|
540
|
+
当一个字段变化时,自动加载另一个字段的选项:
|
|
541
|
+
|
|
542
|
+
```javascript
|
|
543
|
+
{
|
|
544
|
+
field: 'province',
|
|
545
|
+
title: '省份',
|
|
546
|
+
editor: 'select',
|
|
547
|
+
options: [
|
|
548
|
+
{ label: '北京', value: 'beijing' },
|
|
549
|
+
{ label: '广东', value: 'guangdong' }
|
|
550
|
+
],
|
|
551
|
+
linkage: {
|
|
552
|
+
targetField: 'city', // 联动的目标字段
|
|
553
|
+
clearTarget: true, // 联动时是否清空目标字段(默认 true)
|
|
554
|
+
async method(value, row, type) {
|
|
555
|
+
// value: 当前字段值
|
|
556
|
+
// row: 当前行数据
|
|
557
|
+
const { data } = await api.getCityList({ province: value })
|
|
558
|
+
return data.map(item => ({
|
|
559
|
+
label: item.cityName,
|
|
560
|
+
value: item.cityId
|
|
561
|
+
}))
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
},
|
|
565
|
+
{
|
|
566
|
+
field: 'city',
|
|
567
|
+
title: '城市',
|
|
568
|
+
editor: 'select',
|
|
569
|
+
options: [] // 选项由联动动态填充
|
|
570
|
+
}
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
### 4. API 分页请求
|
|
574
|
+
|
|
575
|
+
传入 `apiFunction` 后自动启用分页:
|
|
576
|
+
|
|
577
|
+
```vue
|
|
578
|
+
<DtTable
|
|
579
|
+
:grid-options="gridOptions"
|
|
580
|
+
:api-function="getList"
|
|
581
|
+
:api-params="{ type: 1 }"
|
|
582
|
+
:immediate="true"
|
|
583
|
+
:pagination.sync="pagination"
|
|
584
|
+
@page-change="handlePageChange"
|
|
585
|
+
@size-change="handleSizeChange"
|
|
586
|
+
/>
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
```javascript
|
|
590
|
+
import { getList } from '@/api/list'
|
|
591
|
+
|
|
592
|
+
export default {
|
|
593
|
+
data() {
|
|
594
|
+
return {
|
|
595
|
+
pagination: {
|
|
596
|
+
currentPage: 1,
|
|
597
|
+
pageSize: 10
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
},
|
|
601
|
+
methods: {
|
|
602
|
+
// API 函数需要返回 { list: [], count: Number }
|
|
603
|
+
getList
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
### 5. 自定义渲染(h 函数)
|
|
609
|
+
|
|
610
|
+
使用 Vue 的 `h` 函数渲染复杂内容:
|
|
611
|
+
|
|
612
|
+
```javascript
|
|
613
|
+
{
|
|
614
|
+
field: 'action',
|
|
615
|
+
title: '操作',
|
|
616
|
+
editor: 'custom',
|
|
617
|
+
render: ({ row, h }) => {
|
|
618
|
+
return h('div', null, [
|
|
619
|
+
h('el-button', {
|
|
620
|
+
props: { type: 'text', size: 'small' },
|
|
621
|
+
on: {
|
|
622
|
+
click: () => console.log('查看', row)
|
|
623
|
+
}
|
|
624
|
+
}, '查看'),
|
|
625
|
+
h('el-button', {
|
|
626
|
+
props: { type: 'text', size: 'small' },
|
|
627
|
+
style: { color: '#F56C6C' },
|
|
628
|
+
on: {
|
|
629
|
+
click: () => console.log('删除', row)
|
|
630
|
+
}
|
|
631
|
+
}, '删除')
|
|
632
|
+
])
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
### 6. 表单验证
|
|
638
|
+
|
|
639
|
+
为列配置 `rules` 属性启用验证:
|
|
640
|
+
|
|
641
|
+
```javascript
|
|
642
|
+
{
|
|
643
|
+
field: 'name',
|
|
644
|
+
title: '姓名',
|
|
645
|
+
editor: 'input',
|
|
646
|
+
rules: [
|
|
647
|
+
{ required: true, message: '请输入姓名', trigger: 'blur' },
|
|
648
|
+
{ min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
|
|
649
|
+
]
|
|
650
|
+
}
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
---
|
|
654
|
+
|
|
655
|
+
## 完整示例
|
|
656
|
+
|
|
657
|
+
```vue
|
|
658
|
+
<template>
|
|
659
|
+
<div>
|
|
660
|
+
<el-button @click="handleAdd">新增</el-button>
|
|
661
|
+
<el-button @click="handleBatchDelete">批量删除</el-button>
|
|
662
|
+
<el-button @click="handleSubmit">提交</el-button>
|
|
663
|
+
|
|
664
|
+
<DtTable
|
|
665
|
+
ref="dtTable"
|
|
666
|
+
:grid-options="gridOptions"
|
|
667
|
+
:api-function="getList"
|
|
668
|
+
:api-params="queryParams"
|
|
669
|
+
:pagination.sync="pagination"
|
|
670
|
+
:immediate="true"
|
|
671
|
+
@selection-change="handleSelectionChange"
|
|
672
|
+
/>
|
|
673
|
+
</div>
|
|
674
|
+
</template>
|
|
675
|
+
|
|
676
|
+
<script>
|
|
677
|
+
import DtTable from '@/components/global/DtTable'
|
|
678
|
+
import { getList } from '@/api'
|
|
679
|
+
|
|
680
|
+
export default {
|
|
681
|
+
components: { DtTable },
|
|
682
|
+
data() {
|
|
683
|
+
const self = this
|
|
684
|
+
return {
|
|
685
|
+
queryParams: { type: 1 },
|
|
686
|
+
pagination: { currentPage: 1, pageSize: 10 },
|
|
687
|
+
selectedRows: [],
|
|
688
|
+
gridOptions: {
|
|
689
|
+
columns: [
|
|
690
|
+
{ field: 'selection', title: '' },
|
|
691
|
+
{ field: 'id', title: 'ID', attrs: { width: 80 } },
|
|
692
|
+
{
|
|
693
|
+
field: 'name',
|
|
694
|
+
title: '姓名',
|
|
695
|
+
editor: 'input',
|
|
696
|
+
rules: [
|
|
697
|
+
{ required: true, message: '请输入姓名', trigger: 'blur' }
|
|
698
|
+
],
|
|
699
|
+
attrs: { minWidth: 120 }
|
|
700
|
+
},
|
|
701
|
+
{
|
|
702
|
+
field: 'status',
|
|
703
|
+
title: '状态',
|
|
704
|
+
editor: 'custom',
|
|
705
|
+
render: ({ row, h }) => {
|
|
706
|
+
return h('el-tag', {
|
|
707
|
+
props: { type: row.status === 1 ? 'success' : 'danger' }
|
|
708
|
+
}, row.status === 1 ? '启用' : '禁用')
|
|
709
|
+
},
|
|
710
|
+
attrs: { width: 100 }
|
|
711
|
+
},
|
|
712
|
+
{
|
|
713
|
+
field: 'type',
|
|
714
|
+
title: '类型',
|
|
715
|
+
editor: 'select',
|
|
716
|
+
options: [
|
|
717
|
+
{ label: '类型A', value: 1 },
|
|
718
|
+
{ label: '类型B', value: 2 }
|
|
719
|
+
],
|
|
720
|
+
attrs: { width: 120 }
|
|
721
|
+
},
|
|
722
|
+
{
|
|
723
|
+
field: 'quantity',
|
|
724
|
+
title: '数量',
|
|
725
|
+
editor: 'input-number',
|
|
726
|
+
min: 0,
|
|
727
|
+
max: 999,
|
|
728
|
+
precision: 0,
|
|
729
|
+
unit: '个',
|
|
730
|
+
attrs: { width: 150 }
|
|
731
|
+
},
|
|
732
|
+
{
|
|
733
|
+
field: 'enable',
|
|
734
|
+
title: '启用',
|
|
735
|
+
editor: 'switch',
|
|
736
|
+
attrs: { width: 80 }
|
|
737
|
+
}
|
|
738
|
+
],
|
|
739
|
+
actions: [
|
|
740
|
+
{
|
|
741
|
+
text: '编辑',
|
|
742
|
+
type: 'text',
|
|
743
|
+
click(row, index) {
|
|
744
|
+
self.handleEdit(row)
|
|
745
|
+
}
|
|
746
|
+
},
|
|
747
|
+
{
|
|
748
|
+
text: '删除',
|
|
749
|
+
type: 'text',
|
|
750
|
+
style: { color: '#F56C6C' },
|
|
751
|
+
show: (row) => row.status !== 1,
|
|
752
|
+
click(row, index) {
|
|
753
|
+
self.handleDelete(row, index)
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
],
|
|
757
|
+
actionWidth: '150px',
|
|
758
|
+
scroll: { y: 500 }
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
},
|
|
762
|
+
methods: {
|
|
763
|
+
getList,
|
|
764
|
+
|
|
765
|
+
handleSelectionChange(selection) {
|
|
766
|
+
this.selectedRows = selection
|
|
767
|
+
},
|
|
768
|
+
|
|
769
|
+
handleAdd() {
|
|
770
|
+
const data = this.gridOptions.data || []
|
|
771
|
+
data.push({ id: Date.now(), name: '', status: 1, type: 1, quantity: 0, enable: true })
|
|
772
|
+
this.gridOptions.data = [...data]
|
|
773
|
+
},
|
|
774
|
+
|
|
775
|
+
handleEdit(row) {
|
|
776
|
+
console.log('编辑:', row)
|
|
777
|
+
},
|
|
778
|
+
|
|
779
|
+
handleDelete(row, index) {
|
|
780
|
+
this.$confirm('确认删除?').then(() => {
|
|
781
|
+
this.gridOptions.data.splice(index, 1)
|
|
782
|
+
})
|
|
783
|
+
},
|
|
784
|
+
|
|
785
|
+
handleBatchDelete() {
|
|
786
|
+
const selection = this.$refs.dtTable.getSelection()
|
|
787
|
+
if (selection.length === 0) {
|
|
788
|
+
this.$message.warning('请先选择数据')
|
|
789
|
+
return
|
|
790
|
+
}
|
|
791
|
+
// 批量删除逻辑
|
|
792
|
+
},
|
|
793
|
+
|
|
794
|
+
async handleSubmit() {
|
|
795
|
+
try {
|
|
796
|
+
await this.$refs.dtTable.validate()
|
|
797
|
+
console.log('提交数据:', this.gridOptions.data)
|
|
798
|
+
} catch (error) {
|
|
799
|
+
console.log('验证失败')
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
</script>
|
|
805
|
+
```
|
|
806
|
+
|
|
807
|
+
---
|
|
808
|
+
|
|
809
|
+
## 注意事项
|
|
810
|
+
|
|
811
|
+
1. **数据响应式**:`gridOptions.data` 需要保持响应式,修改时建议使用展开运算符 `[...data]` 触发更新。
|
|
812
|
+
|
|
813
|
+
2. **字段唯一性**:每列的 `field` 必须唯一,`selection` 为保留字段用于多选列。
|
|
814
|
+
|
|
815
|
+
3. **验证规则**:`rules` 遵循 Element UI 的表单验证规则格式。
|
|
816
|
+
|
|
817
|
+
4. **h 函数使用**:render 函数中通过参数 `h` 获取,无需手动导入。
|
|
818
|
+
|
|
819
|
+
5. **异步选项**:使用 `asyncOptions` 时,选项数据会缓存在行数据的 `_asyncOptions` 中。
|