1- import { computed , getCurrentInstance , onMounted , reactive , ref , toRefs , watch } from 'vue-demi '
1+ import http from '@/utils/http '
22import { FaMessageBox } from 'faim'
33import { cloneDeep , merge , mergeWith } from 'lodash-es'
44import qs from 'qs'
5+ import { computed , getCurrentInstance , onMounted , reactive , ref , toRefs , watch } from 'vue-demi'
56// import VueCompositionAPI from '@vue/composition-api'
67import useAdmate from '../../src'
7- import http from '@/utils/http'
88
99// Vue@2.6 or earlier only
1010// Vue.use(VueCompositionAPI)
1111
1212export default (
1313 admateConfig ,
1414 {
15- // 列表筛选参数的初始值,用于动态获取的参数,比如时间
16- // 时间类的参数,如果直接绑定在 list.filter 中,在重置时,时间不会更新
17- // 所以需要调方法动态获取
18- // 可访问 this(组件实例)
19- initialListFilter = function ( ) { } ,
20-
2115 // 获取列表筛选项的表单 ref
2216 // 可访问 this(组件实例)
23- getElFormRefOfListFilter = function ( ) {
24- return this . $refs . listFilterRef
17+ getListFilterRef = function ( ) {
18+ return this ? .$refs . listFilterRef
2519 } ,
2620
2721 // 校验列表筛选项
2822 // 可访问 this(组件实例)
2923 validateListFilter = function ( ) {
30- return getElFormRefOfListFilter ( ) ?. validate ( )
24+ return getListFilterRef ( ) ?. validate ( )
3125 } ,
3226
3327 // 清除列表筛选项校验
3428 // 可访问 this(组件实例)
35- clearValidateOfListFilter = function ( ) {
36- return getElFormRefOfListFilter ( ) ?. clearValidate ( )
29+ clearListFilterValidate = function ( ) {
30+ return getListFilterRef ( ) ?. clearValidate ( )
3731 } ,
3832
3933 // 重置列表筛选项
4034 // 可访问 this(组件实例)
41- resetListFilter = function ( ) {
42- return getElFormRefOfListFilter ( ) . resetFields ( )
35+ resetListFilterFields = function ( ) {
36+ return getListFilterRef ( ) . resetFields ( )
4337 } ,
4438
4539 // 是否在初始化时读取列表
4640 readListImmediately = true ,
4741
48- // 表单标题
49- formTitleMap = {
50- create : '新增' ,
51- read : '查看' ,
52- update : '编辑' ,
53- } ,
54-
5542 // 获取详情的表单 ref
5643 // 可访问 this(组件实例)
5744 // this 判空原因:只有表单没有列表时,openForm 会在 setup 时执行
58- getElFormRefOfFormData = function ( ) {
45+ getFormDataRef = function ( ) {
5946 return this ?. $refs . faFormDialogRef . $refs . elFormRef
6047 } ,
6148
6249 // 校验详情表单
6350 // 可访问 this(组件实例)
6451 validateFormData = function ( ) {
65- return getElFormRefOfFormData ( ) . validate ( )
52+ return getFormDataRef ( ) . validate ( )
6653 } ,
6754
6855 // 清除详情表单校验
6956 // 可访问 this(组件实例)
70- clearValidateOfFormData = function ( ) {
71- return getElFormRefOfFormData ( ) ?. clearValidate ( )
57+ clearFormDataValidate = function ( ) {
58+ return getFormDataRef ( ) ?. clearValidate ( )
7259 } ,
7360
7461 // 自定义钩子函数 - 读取列表后
7562 // 参数1为接口返回值,参数2为触发动机
7663 // 可访问 this(组件实例)
7764 onListRead = function ( ) { } ,
7865
66+ // 自定义钩子函数 - 重置列表后
67+ // 可用于重设动态获取的参数,比如时间
68+ // 时间类的参数,如果直接绑定在 list.filter 中,在重置时,时间不会更新
69+ // 所以需要调函数动态获取
70+ // 可访问 this(组件实例)
71+ onListReset = function ( ) { } ,
72+
7973 // 自定义钩子函数 - 读取表单后(新增时不触发)
8074 // 参数为接口返回值
8175 // 可访问 this(组件实例)
@@ -146,9 +140,6 @@ export default (
146140 pageSize : 10 ,
147141 } ,
148142
149- // 动态的默认参数
150- ...initialListFilter ( ) ,
151-
152143 // 支持路由传参
153144 // 因为 qs 支持数组,所以没有使用 vue-router
154145 // 跳转方式:
@@ -165,7 +156,8 @@ export default (
165156 return
166157 }
167158
168- function readListWithHook ( ) {
159+ async function readListWithHook ( ) {
160+ await validateListFilter ( )
169161 return readList ( ) . then ( ( data ) => {
170162 onListRead ( data , trigger )
171163 return data
@@ -190,9 +182,18 @@ export default (
190182 return readListWithHook ( )
191183 }
192184 } ,
185+ reset ( ) {
186+ resetListFilterFields ( )
187+ onListReset ( )
188+ // 如果分页组件不归属于表单,则表单重置时页码不会被重置,需调用 list.search
189+ if ( ! list . watchFilter ) {
190+ list . search ( )
191+ }
192+ } ,
193193 } ,
194194 } ,
195195 form : {
196+ title : computed ( ( ) => ( { create : '新增' , read : '查看' , update : '编辑' } [ form . status ] ) ) ,
196197 // dataAt: "data",
197198 // 接口返回值中嵌套的对象可能为 null,会覆盖默认值中的空对象/空数组
198199 mergeData ( newFormData ) {
@@ -276,7 +277,7 @@ export default (
276277
277278 // 回显表单后,清除校验
278279 setTimeout ( ( ) => {
279- clearValidateOfFormData ( )
280+ clearFormDataValidate ( )
280281 } , 0 )
281282
282283 return endState
@@ -288,14 +289,14 @@ export default (
288289 ( n ) => {
289290 if ( ! n ) {
290291 setTimeout ( ( ) => {
291- clearValidateOfFormData ( )
292+ clearFormDataValidate ( )
292293 } , 150 )
293294 }
294295 } ,
295296 )
296297
297298 function initializeListFilter ( ) {
298- const elFormRefOfListFilter = getElFormRefOfListFilter ( )
299+ const elFormRefOfListFilter = getListFilterRef ( )
299300 if ( elFormRefOfListFilter ) {
300301 // Object.defineProperty 对不存在的属性无法拦截
301302 list . filter = reactive ( {
@@ -314,20 +315,16 @@ export default (
314315 onMounted ( ( ) => {
315316 currentInstance . value = getCurrentInstance ( ) . proxy
316317
317- initialListFilter = initialListFilter . bind ( currentInstance . value )
318- getElFormRefOfListFilter = getElFormRefOfListFilter . bind (
319- currentInstance . value ,
320- )
318+ getListFilterRef = getListFilterRef . bind ( currentInstance . value )
321319 validateListFilter = validateListFilter . bind ( currentInstance . value )
322- resetListFilter = resetListFilter . bind ( currentInstance . value )
320+ resetListFilterFields = resetListFilterFields . bind ( currentInstance . value )
323321
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 )
328324 validateFormData = validateFormData . bind ( currentInstance . value )
329325
330326 onListRead = onListRead . bind ( currentInstance . value )
327+ onListReset = onListReset . bind ( currentInstance . value )
331328 onFormRead = onFormRead . bind ( currentInstance . value )
332329 onFormOpened = onFormOpened . bind ( currentInstance . value )
333330 onFormSubmit = onFormSubmit . bind ( currentInstance . value )
@@ -347,39 +344,11 @@ export default (
347344 // 校验列表筛选项
348345 validateListFilter,
349346 // 清除列表筛选项校验
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,
375348 // 表单
376349 form,
377350 // 详情的 ref
378351 faFormDialogRef,
379- // 表单标题
380- formTitle : computed ( ( ) => formTitleMap [ form . status ] ) ,
381- // 表单标题字典
382- formTitleMap,
383352 } ) ,
384353 )
385354}
0 commit comments