crud.js
7.37 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
import {
initData,
download
} from '@/api/data'
import {
parseTime,
downloadFile
} from '@/utils/index'
export default {
data() {
return {
// 表格数据
data: [],
// 排序规则,默认 id 降序, 支持多字段排序 ['id,desc', 'createTime,asc']
sort: ['id,desc'],
// 页码
page: 0,
// 每页数据条数
size: 10,
// 总数据条数
total: 0,
// 请求数据的url
url: '',
// 查询数据的参数
params: {},
// 待查询的对象
query: {},
// 等待时间
time: 50,
// 是否为新增类型的表单
isAdd: false,
// 导出的 Loading
downloadLoading: false,
// 表格 Loading 属性
loading: true,
// 删除 Loading 属性
delLoading: false,
delAllLoading: false,
// 弹窗属性
dialog: false,
// Form 表单
form: {},
// 重置表单
resetForm: {},
// 标题
title: ''
}
},
methods: {
parseTime,
downloadFile,
async init() {
if (!await this.beforeInit()) {
return
}
return new Promise((resolve, reject) => {
this.loading = true
// 请求数据
initData(this.url, this.getQueryParame()).then(data => {
this.total = data.totalElements
this.data = data.content
// time 毫秒后显示表格
setTimeout(() => {
this.loading = false
}, this.time)
resolve(data)
}).catch(err => {
this.loading = false
reject(err)
})
})
},
beforeInit() {
return true
},
getQueryParame: function() {
return {
page: this.page,
size: this.size,
sort: this.sort,
...this.query,
...this.params
}
},
// 改变页码
pageChange(e) {
this.page = e - 1
this.init()
},
// 改变每页显示数
sizeChange(e) {
this.page = 0
this.size = e
this.init()
},
// 预防删除第二页最后一条数据时,或者多选删除第二页的数据时,页码错误导致请求无数据
dleChangePage(size) {
if (size === undefined) {
size = 1
}
if (this.data.length === size && this.page !== 0) {
this.page = this.page - 1
}
},
// 查询方法
toQuery() {
this.page = 0
this.init()
},
/**
* 通用的提示封装
*/
submitSuccessNotify() {
this.$notify({
title: '提交成功',
type: 'success',
duration: 2500
})
},
addSuccessNotify() {
this.$notify({
title: '新增成功',
type: 'success',
duration: 2500
})
},
editSuccessNotify() {
this.$notify({
title: '编辑成功',
type: 'success',
duration: 2500
})
},
delSuccessNotify() {
this.$notify({
title: '删除成功',
type: 'success',
duration: 2500
})
},
notify(title, type) {
this.$notify({
title: title,
type: type,
duration: 2500
})
},
/**
* 删除前可以调用 beforeDelMethod 做一些操作
*/
beforeDelMethod() {
return true
},
/**
* 通用的删除
*/
delMethod(id) {
if (!this.beforeDelMethod()) {
return
}
this.delLoading = true
this.crudMethod.del(id).then(() => {
this.delLoading = false
this.$refs[id].doClose()
this.dleChangePage()
this.delSuccessNotify()
this.afterDelMethod()
this.init()
}).catch(() => {
this.delLoading = false
this.$refs[id].doClose()
})
},
afterDelMethod() {},
/**
* 多选删除提示
*/
beforeDelAllMethod() {
this.$confirm('你确定删除选中的数据吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.delAllMethod()
})
},
/**
* 多选删除
*/
delAllMethod() {
this.delAllLoading = true
const data = this.$refs.table.selection
const ids = []
for (let i = 0; i < data.length; i++) {
ids.push(data[i].id)
}
this.crudMethod.delAll(ids).then(() => {
this.delAllLoading = false
this.dleChangePage(ids.length)
this.init()
this.$notify({
title: '删除成功',
type: 'success',
duration: 2500
})
}).catch(() => {
this.delAllLoading = false
})
},
/**
* 显示新增弹窗前可以调用该方法
*/
beforeShowAddForm() {},
/**
* 显示新增弹窗
*/
showAddFormDialog() {
this.isAdd = true
this.resetForm = JSON.parse(JSON.stringify(this.form))
this.beforeShowAddForm()
this.dialog = true
},
/**
* 显示编辑弹窗前可以调用该方法
*/
beforeShowEditForm(data) {},
/**
* 显示编辑弹窗
*/
showEditFormDialog(data = '') {
this.isAdd = false
if (data) {
this.resetForm = JSON.parse(JSON.stringify(this.form))
this.form = JSON.parse(JSON.stringify(data))
}
this.beforeShowEditForm(data)
this.dialog = true
},
/**
* 新增方法
*/
addMethod() {
this.crudMethod.add(this.form).then(() => {
this.addSuccessNotify()
this.loading = false
this.afterAddMethod()
this.cancel()
this.init()
}).catch(() => {
this.loading = false
this.afterAddErrorMethod()
})
},
/**
* 新增后可以调用该方法
*/
afterAddMethod() {},
/**
* 新增失败后调用该方法
*/
afterAddErrorMethod() {},
/**
* 通用的编辑方法
*/
editMethod() {
this.crudMethod.edit(this.form).then(() => {
this.editSuccessNotify()
this.loading = false
this.afterEditMethod()
this.cancel()
this.init()
}).catch(() => {
this.loading = false
})
},
/**
* 编辑后可以调用该方法
*/
afterEditMethod() {},
/**
* 提交前可以调用该方法
*/
beforeSubmitMethod() {
return true
},
/**
* 提交
*/
submitMethod() {
if (!this.beforeSubmitMethod()) {
return
}
if (this.$refs['form']) {
this.$refs['form'].validate((valid) => {
if (valid) {
this.loading = true
if (this.isAdd) {
this.addMethod()
} else this.editMethod()
}
})
}
},
/**
* 隐藏弹窗
*/
cancel() {
this.dialog = false
if (this.$refs['form']) {
this.$refs['form'].clearValidate()
this.form = this.resetForm
}
},
/**
* 获取弹窗的标题
*/
getFormTitle() {
return this.isAdd ? `新增${this.title}` : `编辑${this.title}`
},
/**
* 通用导出
*/
downloadMethod() {
this.beforeInit()
this.downloadLoading = true
download(this.url + '/download', this.params).then(result => {
this.downloadFile(result, this.title + '数据', 'xlsx')
this.downloadLoading = false
}).catch(() => {
this.downloadLoading = false
})
}
}
}