1
0
mirror of https://github.com/PanJiaChen/vue-element-admin.git synced 2026-06-05 03:18:19 +08:00

fix(表格): 修复删除和导出功能并优化总数计算

- 修复删除操作后总数未更新的问题
- 合并导出功能代码并添加错误处理
- 为导出文件添加时间戳后缀
- 移除重复的formatJsonForSelected方法
This commit is contained in:
tyro880 2026-04-18 12:45:29 +08:00
parent 64e2ddf10c
commit 40e466b146

View File

@ -366,6 +366,7 @@ export default {
duration: 2000 duration: 2000
}) })
this.list.splice(index, 1) this.list.splice(index, 1)
this.total = Math.max(0, this.total - 1)
}, },
handleFetchPv(pv) { handleFetchPv(pv) {
fetchPv(pv).then(response => { fetchPv(pv).then(response => {
@ -376,19 +377,39 @@ export default {
handleDownload() { handleDownload() {
this.downloadLoading = true this.downloadLoading = true
import('@/vendor/Export2Excel').then(excel => { import('@/vendor/Export2Excel').then(excel => {
const tHeader = ['timestamp', 'title', 'type', 'importance', 'status'] try {
const filterVal = ['timestamp', 'title', 'type', 'importance', 'status'] const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
const data = this.formatJson(filterVal) const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
excel.export_json_to_excel({ const data = this.formatJson(filterVal)
header: tHeader, const timestamp = parseTime(new Date(), '{y}{m}{d}-{h}{i}{s}')
data, excel.export_json_to_excel({
filename: 'table-list' header: tHeader,
}) data,
filename: `article-list-${timestamp}`
})
this.$message({
message: '导出成功',
type: 'success'
})
} catch (error) {
this.$message({
message: '导出失败',
type: 'error'
})
} finally {
this.downloadLoading = false
}
}).catch(() => {
this.downloadLoading = false this.downloadLoading = false
this.$message({
message: '导出失败',
type: 'error'
})
}) })
}, },
formatJson(filterVal) { formatJson(filterVal, dataSource) {
return this.list.map(v => filterVal.map(j => { const data = dataSource || this.list
return data.map(v => filterVal.map(j => {
if (j === 'timestamp') { if (j === 'timestamp') {
return parseTime(v[j]) return parseTime(v[j])
} else { } else {
@ -421,6 +442,7 @@ export default {
const ids = this.selectedList.map(item => item.id) const ids = this.selectedList.map(item => item.id)
const newList = this.list.filter(item => !ids.includes(item.id)) const newList = this.list.filter(item => !ids.includes(item.id))
this.list = newList this.list = newList
this.total = Math.max(0, this.total - ids.length)
this.selectedList = [] this.selectedList = []
this.$notify({ this.$notify({
title: 'Success', title: 'Success',
@ -443,25 +465,35 @@ export default {
this.batchExportLoading = true this.batchExportLoading = true
import('@/vendor/Export2Excel').then(excel => { import('@/vendor/Export2Excel').then(excel => {
const tHeader = ['timestamp', 'title', 'type', 'importance', 'status'] try {
const filterVal = ['timestamp', 'title', 'type', 'importance', 'status'] const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
const data = this.formatJsonForSelected(filterVal) const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
excel.export_json_to_excel({ const data = this.formatJson(filterVal, this.selectedList)
header: tHeader, const timestamp = parseTime(new Date(), '{y}{m}{d}-{h}{i}{s}')
data, excel.export_json_to_excel({
filename: 'selected-table-list' header: tHeader,
}) data,
this.batchExportLoading = false filename: `article-list-${timestamp}`
}) })
}, this.$message({
formatJsonForSelected(filterVal) { message: '导出成功',
return this.selectedList.map(v => filterVal.map(j => { type: 'success'
if (j === 'timestamp') { })
return parseTime(v[j]) } catch (error) {
} else { this.$message({
return v[j] message: '导出失败',
type: 'error'
})
} finally {
this.batchExportLoading = false
} }
})) }).catch(() => {
this.batchExportLoading = false
this.$message({
message: '导出失败',
type: 'error'
})
})
} }
} }
} }