1
- import { computed , getCurrentInstance , onMounted , reactive , ref , toRefs , watch } from 'vue-demi '
1
+ import http from '@/utils/http '
2
2
import { FaMessageBox } from 'faim'
3
3
import { cloneDeep , merge , mergeWith } from 'lodash-es'
4
4
import qs from 'qs'
5
+ import { computed , getCurrentInstance , onMounted , reactive , ref , toRefs , watch } from 'vue-demi'
5
6
// import VueCompositionAPI from '@vue/composition-api'
6
7
import useAdmate from '../../src'
7
- import http from '@/utils/http'
8
8
9
9
// Vue@2.6 or earlier only
10
10
// Vue.use(VueCompositionAPI)
11
11
12
12
export default (
13
13
admateConfig ,
14
14
{
15
- // 列表筛选参数的初始值,用于动态获取的参数,比如时间
16
- // 时间类的参数,如果直接绑定在 list.filter 中,在重置时,时间不会更新
17
- // 所以需要调方法动态获取
18
- // 可访问 this(组件实例)
19
- initialListFilter = function ( ) { } ,
20
-
21
15
// 获取列表筛选项的表单 ref
22
16
// 可访问 this(组件实例)
23
- getElFormRefOfListFilter = function ( ) {
24
- return this . $refs . listFilterRef
17
+ getListFilterRef = function ( ) {
18
+ return this ? .$refs . listFilterRef
25
19
} ,
26
20
27
21
// 校验列表筛选项
28
22
// 可访问 this(组件实例)
29
23
validateListFilter = function ( ) {
30
- return getElFormRefOfListFilter ( ) ?. validate ( )
24
+ return getListFilterRef ( ) ?. validate ( )
31
25
} ,
32
26
33
27
// 清除列表筛选项校验
34
28
// 可访问 this(组件实例)
35
- clearValidateOfListFilter = function ( ) {
36
- return getElFormRefOfListFilter ( ) ?. clearValidate ( )
29
+ clearListFilterValidate = function ( ) {
30
+ return getListFilterRef ( ) ?. clearValidate ( )
37
31
} ,
38
32
39
33
// 重置列表筛选项
40
34
// 可访问 this(组件实例)
41
- resetListFilter = function ( ) {
42
- return getElFormRefOfListFilter ( ) . resetFields ( )
35
+ resetListFilterFields = function ( ) {
36
+ return getListFilterRef ( ) . resetFields ( )
43
37
} ,
44
38
45
39
// 是否在初始化时读取列表
46
40
readListImmediately = true ,
47
41
48
- // 表单标题
49
- formTitleMap = {
50
- create : '新增' ,
51
- read : '查看' ,
52
- update : '编辑' ,
53
- } ,
54
-
55
42
// 获取详情的表单 ref
56
43
// 可访问 this(组件实例)
57
44
// this 判空原因:只有表单没有列表时,openForm 会在 setup 时执行
58
- getElFormRefOfFormData = function ( ) {
45
+ getFormDataRef = function ( ) {
59
46
return this ?. $refs . faFormDialogRef . $refs . elFormRef
60
47
} ,
61
48
62
49
// 校验详情表单
63
50
// 可访问 this(组件实例)
64
51
validateFormData = function ( ) {
65
- return getElFormRefOfFormData ( ) . validate ( )
52
+ return getFormDataRef ( ) . validate ( )
66
53
} ,
67
54
68
55
// 清除详情表单校验
69
56
// 可访问 this(组件实例)
70
- clearValidateOfFormData = function ( ) {
71
- return getElFormRefOfFormData ( ) ?. clearValidate ( )
57
+ clearFormDataValidate = function ( ) {
58
+ return getFormDataRef ( ) ?. clearValidate ( )
72
59
} ,
73
60
74
61
// 自定义钩子函数 - 读取列表后
75
62
// 参数1为接口返回值,参数2为触发动机
76
63
// 可访问 this(组件实例)
77
64
onListRead = function ( ) { } ,
78
65
66
+ // 自定义钩子函数 - 重置列表后
67
+ // 可用于重设动态获取的参数,比如时间
68
+ // 时间类的参数,如果直接绑定在 list.filter 中,在重置时,时间不会更新
69
+ // 所以需要调函数动态获取
70
+ // 可访问 this(组件实例)
71
+ onListReset = function ( ) { } ,
72
+
79
73
// 自定义钩子函数 - 读取表单后(新增时不触发)
80
74
// 参数为接口返回值
81
75
// 可访问 this(组件实例)
@@ -146,9 +140,6 @@ export default (
146
140
pageSize : 10 ,
147
141
} ,
148
142
149
- // 动态的默认参数
150
- ...initialListFilter ( ) ,
151
-
152
143
// 支持路由传参
153
144
// 因为 qs 支持数组,所以没有使用 vue-router
154
145
// 跳转方式:
@@ -165,7 +156,8 @@ export default (
165
156
return
166
157
}
167
158
168
- function readListWithHook ( ) {
159
+ async function readListWithHook ( ) {
160
+ await validateListFilter ( )
169
161
return readList ( ) . then ( ( data ) => {
170
162
onListRead ( data , trigger )
171
163
return data
@@ -190,9 +182,18 @@ export default (
190
182
return readListWithHook ( )
191
183
}
192
184
} ,
185
+ reset ( ) {
186
+ resetListFilterFields ( )
187
+ onListReset ( )
188
+ // 如果分页组件不归属于表单,则表单重置时页码不会被重置,需调用 list.search
189
+ if ( ! list . watchFilter ) {
190
+ list . search ( )
191
+ }
192
+ } ,
193
193
} ,
194
194
} ,
195
195
form : {
196
+ title : computed ( ( ) => ( { create : '新增' , read : '查看' , update : '编辑' } [ form . status ] ) ) ,
196
197
// dataAt: "data",
197
198
// 接口返回值中嵌套的对象可能为 null,会覆盖默认值中的空对象/空数组
198
199
mergeData ( newFormData ) {
@@ -276,7 +277,7 @@ export default (
276
277
277
278
// 回显表单后,清除校验
278
279
setTimeout ( ( ) => {
279
- clearValidateOfFormData ( )
280
+ clearFormDataValidate ( )
280
281
} , 0 )
281
282
282
283
return endState
@@ -288,14 +289,14 @@ export default (
288
289
( n ) => {
289
290
if ( ! n ) {
290
291
setTimeout ( ( ) => {
291
- clearValidateOfFormData ( )
292
+ clearFormDataValidate ( )
292
293
} , 150 )
293
294
}
294
295
} ,
295
296
)
296
297
297
298
function initializeListFilter ( ) {
298
- const elFormRefOfListFilter = getElFormRefOfListFilter ( )
299
+ const elFormRefOfListFilter = getListFilterRef ( )
299
300
if ( elFormRefOfListFilter ) {
300
301
// Object.defineProperty 对不存在的属性无法拦截
301
302
list . filter = reactive ( {
@@ -314,20 +315,16 @@ export default (
314
315
onMounted ( ( ) => {
315
316
currentInstance . value = getCurrentInstance ( ) . proxy
316
317
317
- initialListFilter = initialListFilter . bind ( currentInstance . value )
318
- getElFormRefOfListFilter = getElFormRefOfListFilter . bind (
319
- currentInstance . value ,
320
- )
318
+ getListFilterRef = getListFilterRef . bind ( currentInstance . value )
321
319
validateListFilter = validateListFilter . bind ( currentInstance . value )
322
- resetListFilter = resetListFilter . bind ( currentInstance . value )
320
+ resetListFilterFields = resetListFilterFields . bind ( currentInstance . value )
323
321
324
- getElFormRefOfFormData = getElFormRefOfFormData . bind ( currentInstance . value )
325
- clearValidateOfFormData = clearValidateOfFormData . bind (
326
- currentInstance . value ,
327
- )
322
+ getFormDataRef = getFormDataRef . bind ( currentInstance . value )
323
+ clearFormDataValidate = clearFormDataValidate . bind ( currentInstance . value )
328
324
validateFormData = validateFormData . bind ( currentInstance . value )
329
325
330
326
onListRead = onListRead . bind ( currentInstance . value )
327
+ onListReset = onListReset . bind ( currentInstance . value )
331
328
onFormRead = onFormRead . bind ( currentInstance . value )
332
329
onFormOpened = onFormOpened . bind ( currentInstance . value )
333
330
onFormSubmit = onFormSubmit . bind ( currentInstance . value )
@@ -347,39 +344,11 @@ export default (
347
344
// 校验列表筛选项
348
345
validateListFilter,
349
346
// 清除列表筛选项校验
350
- clearValidateOfListFilter,
351
- // 重置列表
352
- resetList : ( ) => {
353
- resetListFilter ( )
354
- if ( initialListFilter ) {
355
- list . filter = {
356
- ...list . filter ,
357
- ...initialListFilter ( ) ,
358
- }
359
- }
360
- if ( ! list . watchFilter ) {
361
- list . read ( )
362
- }
363
- } ,
364
- // 校验筛选项并读取列表首页
365
- queryList : async ( ...args ) => {
366
- await validateListFilter ( )
367
- if ( list . watchFilter && list . filter . page . pageNo !== 1 ) {
368
- list . filter . page . pageNo = 1
369
- }
370
- else {
371
- list . filter . page . pageNo = 1
372
- list . read ( ...args )
373
- }
374
- } ,
347
+ clearListFilterValidate,
375
348
// 表单
376
349
form,
377
350
// 详情的 ref
378
351
faFormDialogRef,
379
- // 表单标题
380
- formTitle : computed ( ( ) => formTitleMap [ form . status ] ) ,
381
- // 表单标题字典
382
- formTitleMap,
383
352
} ) ,
384
353
)
385
354
}
0 commit comments