Blame view

ant-design-vue-jeecg/src/utils/JEditableTableUtil.js 4.45 KB
肖超群 authored
1
import {getVmParentByName} from '@/utils/util'
肖超群 authored
2
3
4
5
6
7
8
9
10
11
12
13
14

const FormTypes = {
  normal: 'normal',
  input: 'input',
  inputNumber: 'inputNumber',
  checkbox: 'checkbox',
  select: 'select',
  date: 'date',
  datetime: 'datetime',
  time: 'time',
  upload: 'upload',
  file: 'file',
  image: 'image',
肖超群 authored
15
16
17
18
19
20
21
  popup: 'popup',
  list_multi: "list_multi",
  sel_search: "sel_search",
  sel_search_async: "sel_search_async",
  radio: 'radio',
  checkbox_meta: "checkbox_meta",
  input_pop: 'input_pop',
肖超群 authored
22
23
24
25
26
27
  sel_depart: 'sel_depart',
  sel_user: 'sel_user',
  slot: 'slot',
  hidden: 'hidden'
}
const VALIDATE_NO_PASSED = Symbol()
肖超群 authored
28
export {FormTypes, VALIDATE_NO_PASSED}
肖超群 authored
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

/**
 * 获取指定的 $refs 对象
 * 有时候可能会遇到组件未挂载到页面中的情况,导致无法获取 $refs 中的某个对象
 * 这个方法可以等待挂载完成之后再返回 $refs 的对象,避免报错
 * @author sunjianlei
 **/
export function getRefPromise(vm, name) {
  return new Promise((resolve) => {
    (function next() {
      let ref = vm.$refs[name]
      if (ref) {
        resolve(ref)
      } else {
        setTimeout(() => {
          next()
        }, 10)
      }
    })()
  })
}

/**
 * 一次性验证主表单和所有的次表单
 * @param form 主表单 form 对象
 * @param cases 接收一个数组,每项都是一个JEditableTable实例
 * @returns {Promise<any>}
 * @author sunjianlei
 */
export function validateFormAndTables(form, cases) {

  if (!(form && typeof form.validateFields === 'function')) {
    throw `form 参数需要的是一个form对象,而传入的却是${typeof form}`
  }

  let options = {}
  return new Promise((resolve, reject) => {
    // 验证主表表单
    form.validateFields((err, values) => {
肖超群 authored
68
      err ? reject({error: VALIDATE_NO_PASSED}) : resolve(values)
肖超群 authored
69
70
    })
  }).then(values => {
肖超群 authored
71
    Object.assign(options, {formValue: values})
肖超群 authored
72
73
74
    // 验证所有子表的表单
    return validateTables(cases)
  }).then(all => {
肖超群 authored
75
    Object.assign(options, {tablesValue: all})
肖超群 authored
76
77
78
79
80
81
    return Promise.resolve(options)
  }).catch(error => {
    return Promise.reject(error)
  })

}
肖超群 authored
82
肖超群 authored
83
84
85
86
87
88
89
/**
 * 一次性验证主表单和所有的次表单(新版本)
 * @param form 主表单 form 对象
 * @param cases 接收一个数组,每项都是一个JEditableTable实例
 * @returns {Promise<any>}
 * @author sunjianlei
 */
肖超群 authored
90
export function validateFormModelAndTables(form, values, cases) {
肖超群 authored
91
92
93
94
95
96
97

  if (!(form && typeof form.validate === 'function')) {
    throw `form 参数需要的是一个form对象,而传入的却是${typeof form}`
  }
  let options = {}
  return new Promise((resolve, reject) => {
    // 验证主表表单
肖超群 authored
98
99
    form.validate((valid, obj) => {
      valid ? resolve(values) : reject({error: VALIDATE_NO_PASSED})
肖超群 authored
100
101
    })
  }).then(values => {
肖超群 authored
102
    Object.assign(options, {formValue: values})
肖超群 authored
103
104
105
    // 验证所有子表的表单
    return validateTables(cases)
  }).then(all => {
肖超群 authored
106
    Object.assign(options, {tablesValue: all})
肖超群 authored
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    return Promise.resolve(options)
  }).catch(error => {
    return Promise.reject(error)
  })

}

/**
 * 验证并获取一个或多个表格的所有值
 * @param cases 接收一个数组,每项都是一个JEditableTable实例
 * @param deleteTempId 是否删除临时ID,如果设为true,行编辑就不返回新增行的ID,ID需要后台生成
 * @author sunjianlei
 */
export function validateTables(cases, deleteTempId) {
  if (!(cases instanceof Array)) {
    throw `'validateTables'函数的'cases'参数需要的是一个数组,而传入的却是${typeof cases}`
  }
  return new Promise((resolve, reject) => {
    let tables = []
    let index = 0;
肖超群 authored
127
    if (!cases || cases.length === 0) {
肖超群 authored
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
      resolve()
    }
    (function next() {
      let vm = cases[index]
      vm.getAll(true, deleteTempId).then(all => {
        tables[index] = all
        // 判断校验是否全部完成,完成返回成功,否则继续进行下一步校验
        if (++index === cases.length) {
          resolve(tables)
        } else (
          next()
        )
      }, error => {
        // 出现未验证通过的表单,不再进行下一步校验,直接返回失败并跳转到该表格
        if (error === VALIDATE_NO_PASSED) {
          // 尝试获取tabKey,如果在ATab组件内即可获取
          let paneKey;
          let tabPane = getVmParentByName(vm, 'ATabPane')
          if (tabPane) {
            paneKey = tabPane.$vnode.key
          }
          reject({error: VALIDATE_NO_PASSED, index, paneKey})
        }
        reject(error)
      })
    })()
  })
}