-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathViewGridEventButton.jsx
More file actions
470 lines (452 loc) · 13.9 KB
/
ViewGridEventButton.jsx
File metadata and controls
470 lines (452 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import {
getUrl,
initBox,
resetAdd,
resetEditForm,
initOntinueAdd,
modelOpenProcess,
getRemoteFormDefaultKeyValue
} from './ViewGridProvider.jsx'
import { initImportOptions } from './ViewGridInitButtonsAuthFields.jsx'
// import {} './ViewGridProviderDetail.jsx'
import action from './Action.js'
// 新建
export const onAdd = async (proxy, props, dataConfig) => {
//行内编辑
if (props.table.editTable) {
editTableAddRow(proxy, props, {}, -1)
return
}
const boxOptions = dataConfig.boxOptions
boxOptions.title = proxy.$ts(props.table.cnName) + '(' + proxy.$ts('新建') + ')'
dataConfig.currentAction.value = action.ADD
dataConfig.currentRow.value = {}
if (!(await initBox(proxy, props, dataConfig))) return
resetAdd(proxy, props, dataConfig)
initOntinueAdd(proxy, props, dataConfig, true)
dataConfig.boxModel.value = true
modelOpenProcess(proxy, props, dataConfig)
}
//编辑
export const onEdit = async (proxy, props, dataConfig, rows) => {
if (rows) {
if (!Array.isArray(rows)) {
rows = [rows]
}
} else {
rows = proxy.getSelected()
}
if (rows.length == 0) {
return proxy.$error(proxy.$ts('请选择要编辑的行!'))
}
if (rows.length != 1) {
return proxy.$error(proxy.$ts('只能选择一行数据进行编辑!'))
}
dataConfig.boxOptions.title = proxy.$ts('编辑')
//编辑
dataConfig.currentAction.value = action.EDIT
//记录当前编辑的行
dataConfig.currentRow.value = rows[0]
//初始化弹出框
if (!(await initBox(proxy, props, dataConfig))) return
initOntinueAdd(proxy, props, dataConfig, true)
dataConfig.boxModel.value = true
//重置表单
//resetDetailTable(proxy,props,dataConfig, rows[0],null)
//重新表单与明细表数据
resetEditForm(proxy, props, dataConfig, rows[0])
//设置远程查询表单的默认key/value
getRemoteFormDefaultKeyValue(proxy, props, dataConfig)
//点击编辑按钮弹出框后,可以在此处写逻辑,如,从后台获取数据
modelOpenProcess(proxy, props, dataConfig, rows[0])
}
export const onDelete = async (proxy, props, rows) => {
if (rows) {
if (!Array.isArray(rows)) {
rows = [rows]
}
} else {
rows = proxy.getSelectRows() // proxy.$refs.table.getSelected()
}
if (!rows || rows.length === 0) return proxy.$error(proxy.$ts('请选择要删除的行!'))
let delKeys = rows.map((x) => {
return x[props.table.key]
})
if (!delKeys || delKeys.length === 0) return proxy.$error(proxy.$ts('没有获取要删除的行数据!'))
if (
!(await props.delBefore(delKeys, rows)) ||
!(await proxy.delBefore.call(proxy, delKeys, rows))
) {
return
}
if (
!(await props.delBeforeAsync(delKeys, rows)) ||
!(await proxy.delBeforeAsync.call(proxy,delKeys, rows))
) {
return
}
const delMsg =
proxy.getDelMessage.call(proxy, rows) ||
props.getDelMessage(rows) ||
proxy.$ts('确认要删除选择的数据吗?')
let tigger = false
proxy
.$confirm(delMsg, proxy.$ts('警告'), {
confirmButtonText: proxy.$ts('确定'),
cancelButtonText: proxy.$ts('取消'),
dangerouslyUseHTMLString: true,
type: 'warning',
center: true
})
.then(() => {
if (tigger) return
tigger = true
let url = getUrl(action.DEL, null, props.table)
proxy.http.post(url, delKeys, proxy.$ts('正在删除数据') + '....').then((x) => {
if (!x.status) return proxy.$error(x.message)
proxy.$success(x.message)
if (!proxy.delAfter.call(proxy, x, rows)) {
return
}
if (!props.delAfter(x, rows)) {
return
}
proxy.search()
})
})
.catch((action) => {
if (action !== 'cancel') {
console.log(action)
proxy.$error(action)
}
})
}
//保存
export const saveClick = (proxy, props, dataConfig) => {
proxy.$refs.form.validate((result) => {
if (!result) return
saveExecute(proxy, props, dataConfig)
})
}
//保存前确认操作
const saveExecuteConfirm = (proxy, props, formData,dataConfig, callback) => {
const isAdd=dataConfig.currentAction.value=='Add'
proxy.saveConfirm.call(proxy, (res) => {
props.saveConfirm((res) => {
callback()
},formData,isAdd)
},formData,isAdd)
}
const saveExecute = async (proxy, props, dataConfig) => {
let editFormFields = proxy.base.getFormValues(props.editFormFields, props.editFormOptions)
const currentAction = dataConfig.currentAction.value
const currentRow = dataConfig.currentRow.value || {}
const hiddenFields = dataConfig.hiddenFields.value || []
if (currentAction !== action.ADD && hiddenFields.length) {
for (const key in editFormFields) {
if (hiddenFields.indexOf(key) !== -1) {
editFormFields[key] = undefined
}
}
}
let formData = {
mainData: editFormFields,
detailData: null,
delKeys: null
}
const $global = proxy.$global
if ($global.dataVersion) {
formData.dataVersionField = $global.dataVersion
if (currentAction !== 'Add' && $global.dataVersion) {
formData.dataVersionValue = currentRow[$global.dataVersion]
}
}
let details
if (dataConfig.hasDetail.value) {
//获取明细表数据
const rows = proxy.getTable().rowData;
formData.detailData = convertDetailSubmitData(rows, props.detail.columns)
} else if (dataConfig.isMultiple.value) {
details =props.details.map((c) => {
if (c.columns) {
let itemDetail = {
table: c.table,
delKeys: c.delKeys,
data: convertDetailSubmitData(proxy.getTable(c.table).rowData, c.columns)
// data: convertDetailSubmitData(getGridTableRef(proxy, props, c.table).rowData, c.columns)
}
if (dataConfig.submitChangeRows.value) {
itemDetail.data = proxy.$refs.detailsRef.getDiffRows(c.table, c.key, itemDetail.data, c.detail)
// itemDetail.data = $refs.detailsRef.getDiffRows(c.table, c.key, itemDetail.data, c.detail)
}
return itemDetail
}
return {
table: c.table,
delKeys: c.delKeys,
data: []
}
})
formData.details = details
}
const detailOptions = dataConfig.detailOptions
if (detailOptions.delKeys.length > 0) {
formData.delKeys = detailOptions.delKeys
}
const subDetails = dataConfig.subDetails.value
if (subDetails && subDetails.length) {
formData.subDelInfo = subDetails.map((x) => {
return { table: x.table, delKeys: x.delKeys }
})
}
let isAdd = currentAction === action.ADD
if (isAdd) {
if (
!(await proxy.addBefore.call(proxy, formData)) ||
!(await proxy.addBeforeAsync.call(proxy, formData))
)
return
if (!(await props.addBefore(formData)) || !(await props.addBeforeAsync(formData))) return
} else {
if (
!(await proxy.updateBefore.call(proxy, formData)) ||
!(await proxy.updateBeforeAsync.call(proxy, formData))
)
return
if (!(await props.updateBefore(formData)) || !(await props.updateBeforeAsync(formData))) return
}
let url = getUrl(isAdd ? 'add' : 'update', null, props.table)
// resetAdd(proxy, props, dataConfig);
// proxy.$refs.form.$refs.volform.clearValidate()
// return;
saveExecuteConfirm(proxy, props, formData, dataConfig,() => {
proxy.http.post(url, formData, true).then((x) => {
if (isAdd) {
if (!proxy.addAfter.call(proxy, x, formData)) return
if (!props.addAfter(x, formData)) return
//连续添加
if (dataConfig.continueAdd.value && x.status) {
proxy.$success(x.message)
dataConfig.currentAction.value = action.ADD
let _formFields
if (proxy.continueAddAfter) {
_formFields = JSON.parse(JSON.stringify(editFormFields))
}
dataConfig.currentRow.value = {}
proxy.$refs.form.$refs.volform.clearValidate()
resetAdd(proxy, props, dataConfig)
proxy.search()
proxy.continueAddAfter.call(proxy, _formFields, formData, x)
props.continueAddAfter.call(_formFields, formData, x)
return
}
} else {
if (!proxy.updateAfter.call(proxy, x, formData)) return
if (!props.updateAfter(x, formData)) return
}
if (!x.status) return proxy.$error(x.message)
proxy.$success(x.message || proxy.$ts('保存成功'))
if (dataConfig.boxOptions.saveClose) {
// 调用 onGridModelClose 以触发 onModelClose 钩子
if (proxy.onGridModelClose && typeof proxy.onGridModelClose === 'function') {
proxy.onGridModelClose(false)
} else {
dataConfig.boxModel.value = false
}
//$refs.table.load(null, isAdd)
proxy.getTable(true).load(null, isAdd)
return
}
let resultRow
if (typeof x.data === 'string' && x.data !== '') {
resultRow = JSON.parse(x.data)
} else {
resultRow = x.data
}
if (currentAction === action.ADD) {
props.editFormFields[props.table.key] = ''
dataConfig.currentAction.value = action.EDIT
dataConfig.currentRow.value = resultRow.data
}
resetEditForm(proxy, props, dataConfig, resultRow.data)
if (dataConfig.hasDetail.value) {
dataConfig.detailOptions.delKeys = []
if (resultRow.list) {
proxy.getTable(true).rowData.push(...resultRow.list)
// $refs.detail.rowData.push(...resultRow.list)
}
}
proxy.getTable(true).load(null, isAdd)
//$refs.table.load(null, isAdd)
})
})
}
const convertDetailSubmitData = (detailData, columns) => {
const types = ['selectList', 'cascader', 'treeSelect']
let _fields = columns
.filter((c) => {
return types.indexOf(c.type) !== -1 || types.indexOf(c.edit && c.edit.type) !== -1
})
.map((c) => {
return c.field
})
if (_fields.length) {
detailData = JSON.parse(JSON.stringify(detailData))
detailData.forEach((row) => {
for (let index = 0; index < _fields.length; index++) {
const _field = _fields[index]
if (Array.isArray(row[_field])) {
row[_field] = row[_field].join(',')
}
}
})
}
return detailData
}
//导入
export const importData = async (proxy, props, dataConfig, isDetail) => {
const upload = dataConfig.upload
if (!upload.url) {
initImportOptions(proxy, props, dataConfig)
}
upload.excel = true
proxy.$refs.upload_excel?.reset()
}
//导出
export const exportData = async (proxy, props, dataConfig, isDetail) => {
//导出
let url, wheres, param
if (isDetail) {
//明细表导出时如果是新建状态,禁止导出
if (dataConfig.currentAction.value === 'Add') {
return
}
url = `api/${props.detail.table}/${action.EXPORT}`
param = {
wheres: [{ name: props.table.key, value: props.editFormFields[props.table.key] }]
}
} else {
//主表导出
url = getUrl(action.EXPORT, null, props.table)
wheres = proxy.base.getSearchParameters(proxy, props.searchFormFields, props.searchFormOptions)
param = {
order: proxy.$refs.table.paginations.order,
sort: proxy.$refs.table.paginations.sort,
wheres: wheres||[]
}
if (
!param.wheres.some((x) => {
return x.name === props.table.key
})
) {
let ids = proxy
.getSelectRows()
.map((x) => {
return x[props.table.key]
})
.join(',')
//2024.01.13增加默认导出勾选的数据
if (ids) {
param.wheres.push({
name: props.table.key,
value: ids,
displayType: 'selectList'
})
}
}
//2024.02.03增加导出列表与界面显示字段一致
let _columns = []
props.columns.forEach((col) => {
if (!col.hidden && !col.render) {
if (col.children) {
_columns.push(
...col.children
.filter((c) => {
return !c.hidden
})
.map((m) => {
return m.field
})
)
} else {
_columns.push(col.field)
}
}
})
if (_columns.length) {
param.columns = _columns
}
}
//2020.06.25增加导出前处理
if (!isDetail) {
if (!(await props.exportBefore(param))) {
return
}
if (!(await proxy.exportBefore.call(proxy, param))) {
return
}
}
if (param.wheres && typeof param.wheres === 'object') {
param.wheres = JSON.stringify(param.wheres)
}
//2022.09.26增加自定义导出文件名
let fileName = dataConfig.downloadFileName.value
if (!fileName) {
fileName = props.getFileName(isDetail) || proxy.getFileName.call(proxy, isDetail)
}
if (!fileName) {
if (isDetail) {
fileName = proxy.$ts(props.detail.cnName) + '.xlsx'
}
fileName = proxy.$ts(props.table.cnName) + '.xlsx'
}
//url, params, fileName, loading
proxy.http.download(url, param, fileName, 'loading....', (res) => {
if (!props.exportAfter(res, param)) {
return
}
if (!proxy.exportAfter.call(proxy, res, param)) {
return
}
})
}
export const onImportExcelAfter = (proxy, props, dataConfig, data) => {
//2022.01.08增加明细表导入后方法判断
if (!data.status) {
return // this.$message.error(data.message);
}
if (data.data && typeof data.data === 'string') {
data.data = JSON.parse(data.data)
}
//明细表导入
if (dataConfig.boxModel.value) {
if (!data.data) {
data.data = []
}
data.data.forEach((x) => {
x[props.detail.key] = undefined
x[props.table.key] = undefined
})
//增加明细表导入后处理
if (!proxy.importDetailAfter.call(proxy, data)) {
return
}
if (!props.importDetailAfter(data)) {
return
}
proxy.$refs.detail.rowData.unshift(...data.data)
dataConfig.upload.excel = false
//刷新明细表proxy.upd
return
}
//主表导入
if (!proxy.importAfter.call(proxy, data)) {
return
}
if (!props.importAfter(data)) {
return
}
proxy.$message(proxy.$ts('上传成功'))
//刷新主表导入信息
proxy.search()
}