|
1
|
import JEditableTable from '@/components/jeecg/JEditableTable'
|
|
2
3
|
import {VALIDATE_NO_PASSED, getRefPromise, validateFormModelAndTables} from '@/utils/JEditableTableUtil'
import {httpAction, getAction} from '@/api/manage'
|
|
4
|
import { translateResultMessage } from '@/api/api'
|
|
5
6
7
8
9
10
11
|
export const JEditableTableModelMixin = {
components: {
JEditableTable
},
data() {
return {
|
|
12
|
title: this.$t('system.options'),
|
|
13
14
|
visible: false,
confirmLoading: false,
|
|
15
|
model: {},
|
|
16
|
labelCol: {
|
|
17
18
|
xs: {span: 24},
sm: {span: 6}
|
|
19
20
|
},
wrapperCol: {
|
|
21
22
|
xs: {span: 24},
sm: {span: 18}
|
|
23
24
25
26
27
28
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
68
69
70
71
|
}
}
},
methods: {
/** 获取所有的editableTable实例 */
getAllTable() {
if (!(this.refKeys instanceof Array)) {
throw this.throwNotArray('refKeys')
}
let values = this.refKeys.map(key => getRefPromise(this, key))
return Promise.all(values)
},
/** 遍历所有的JEditableTable实例 */
eachAllTable(callback) {
// 开始遍历
this.getAllTable().then(tables => {
tables.forEach((item, index) => {
if (typeof callback === 'function') {
callback(item, index)
}
})
})
},
/** 当点击新增按钮时调用此方法 */
add() {
//update-begin-author:lvdandan date:20201113 for:LOWCOD-1049 JEditaTable,子表默认添加一条数据,addDefaultRowNum设置无效 #1930
return new Promise((resolve) => {
this.tableReset();
resolve();
}).then(() => {
if (typeof this.addBefore === 'function') this.addBefore()
// 默认新增空数据
let rowNum = this.addDefaultRowNum
if (typeof rowNum !== 'number') {
rowNum = 1
console.warn('由于你没有在 data 中定义 addDefaultRowNum 或 addDefaultRowNum 不是数字,所以默认添加一条空数据,如果不想默认添加空数据,请将定义 addDefaultRowNum 为 0')
}
this.eachAllTable((item) => {
item.add(rowNum)
})
if (typeof this.addAfter === 'function') this.addAfter(this.model)
this.edit(this.model)
})
//update-end-author:lvdandan date:20201113 for:LOWCOD-1049 JEditaTable,子表默认添加一条数据,addDefaultRowNum设置无效 #1930
},
/** 当点击了编辑(修改)按钮时调用此方法 */
edit(record) {
|
|
72
|
if (record && '{}' != JSON.stringify(record) && record.id) {
|
|
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
this.tableReset();
}
if (typeof this.editBefore === 'function') this.editBefore(record)
this.visible = true
this.activeKey = this.refKeys[0]
this.$refs.form.resetFields()
this.model = Object.assign({}, record)
if (typeof this.editAfter === 'function') this.editAfter(this.model)
},
/** 关闭弹窗,并将所有JEditableTable实例回归到初始状态 */
close() {
this.visible = false
this.$emit('close')
},
//清空子表table的数据
|
|
88
|
tableReset() {
|
|
89
90
91
92
93
94
95
96
|
this.eachAllTable((item) => {
item.clearRow()
})
},
/** 查询某个tab的数据 */
requestSubTableData(url, params, tab, success) {
tab.loading = true
getAction(url, params).then(res => {
|
|
97
|
let {result} = res
|
|
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
let dataSource = []
if (result) {
if (Array.isArray(result)) {
dataSource = result
} else if (Array.isArray(result.records)) {
dataSource = result.records
}
}
tab.dataSource = dataSource
typeof success === 'function' ? success(res) : ''
}).finally(() => {
tab.loading = false
})
},
/** 发起请求,自动判断是执行新增还是修改操作 */
request(formData) {
let url = this.url.add, method = 'post'
if (this.model.id) {
url = this.url.edit
method = 'put'
}
this.confirmLoading = true
httpAction(url, formData, method).then((res) => {
if (res.success) {
|
|
122
|
this.$message.success(translateResultMessage(res, res.message))
|
|
123
124
125
|
this.$emit('ok')
this.close()
} else {
|
|
126
|
this.$message.warning(translateResultMessage(res, res.message))
|
|
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
}
}).finally(() => {
this.confirmLoading = false
})
},
/* --- handle 事件 --- */
/** ATab 选项卡切换事件 */
handleChangeTabs(key) {
// 自动重置scrollTop状态,防止出现白屏
getRefPromise(this, key).then(editableTable => {
editableTable.resetScrollTop()
})
},
/** 关闭按钮点击事件 */
handleCancel() {
this.close()
},
/** 确定按钮点击事件 */
handleOk() {
/** 触发表单验证 */
this.getAllTable().then(tables => {
/** 一次性验证主表和所有的次表 */
|
|
151
|
return validateFormModelAndTables(this.$refs.form, this.model, tables)
|
|
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
}).then(allValues => {
/** 一次性验证一对一的所有子表 */
return this.validateSubForm(allValues)
}).then(allValues => {
if (typeof this.classifyIntoFormData !== 'function') {
throw this.throwNotFunction('classifyIntoFormData')
}
let formData = this.classifyIntoFormData(allValues)
// 发起请求
return this.request(formData)
}).catch(e => {
if (e.error === VALIDATE_NO_PASSED) {
// 如果有未通过表单验证的子表,就自动跳转到它所在的tab
//update--begin--autor:liusq-----date:20210316------for:未通过表单验证跳转tab问题------
|
|
166
|
this.activeKey = e.index == null ? this.activeKey : (e.paneKey ? e.paneKey : this.refKeys[e.index])
|
|
167
168
169
170
171
172
173
|
//update--end--autor:liusq-----date:20210316------for:未通过表单验证跳转tab问题------
} else {
console.error(e)
}
})
},
//校验所有子表表单
|
|
174
|
validateSubForm(allValues) {
|
|
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
return new Promise((resolve) => {
resolve(allValues)
})
},
/* --- throw --- */
/** not a function */
throwNotFunction(name) {
return `${name} 未定义或不是一个函数`
},
/** not a array */
throwNotArray(name) {
return `${name} 未定义或不是一个数组`
}
}
}
|