<p-table
ref="table"
rowKey="id"
size="middle"
:rowClassName="getRowClassName"
:columns="columns"
:dataSource="dataSource"
:pagination="ipagination"
:loading="loading"
@change="handleTableChange"
:scroll="{ x: 1500, y: scrollY }"
>
<template v-for="item in columns" :slot="item.slotName">
<span class="d-block ellipsis" :key="index" :title="$t(item.slotName)">{{ $t(item.slotName) }}</span>
</template>
</p-table>
import { getAction, downFile, httpAction } from '@/api/manage.js'
import system from '@/config/system'
export const TableMixin = {
data() {
return {
queryParam: {},
dataSource: [],
toggleSearchStatus: false,
selectedRowKeys: [],
selectionRows: [],
loading: false,
ipagination: {
current: 1,
pageSize: 20,
pageSizeOptions: ['10', '20', '30', '50'],
showTotal: (total, range) => {
return range[0] + '-' + range[1] + ' 共' + total + '条'
},
showQuickJumper: true,
showSizeChanger: true,
total: 0
},
isMountedLoad: true,
importLoading: false,
scrollY: 600,
screenHeight: document.body.clientHeight
}
},
computed: {
importExcelUrl() {
return system.devApi + this.url.fileUrl
}
},
mounted() {
if (this.isMountedLoad) {
this.loadData()
}
this.handleTableHeight()
window.addEventListener('resize', this.handleTableHeight)
},
destroyed() {
window.removeEventListener('resize', this.handleTableHeight)
},
methods: {
loadData(arg) {
if (!this.url.list) {
this.$message.error('请设置url.list属性!')
return
}
if (arg === 1) {
this.ipagination.current = 1
}
this.loading = true
let params = this.getQueryParams()
getAction(this.url.list, params).then((res) => {
let resData = res.data
if (resData.success && resData.data) {
this.dataSource = resData.data.records || resData.data
if (this.needChildren) {
this.dataSource = (this.dataSource || []).map((item) => {
item.children = item.children || []
item.isParent = true
return item
})
}
if (resData.data.total) {
this.ipagination.total = resData.data.total
} else {
this.ipagination.total = 0
}
} else {
this.$message.warning(resData.msg || '服务出错,请稍后重试!')
}
this.loading = false
})
},
getQueryParams() {
var param = Object.assign(this.queryParam)
param.pageNo = this.ipagination.current
param.limit = this.ipagination.pageSize
return param
},
searchQuery() {
this.loadData(1)
},
searchReset() {
this.queryParam = {}
this.loadData(1)
},
onSelectChange(selectedRowKeys, selectionRows) {
this.selectedRowKeys = selectedRowKeys
this.selectionRows = selectionRows
},
handleTableChange(pagination, filters, sorter) {
if (Object.keys(sorter).length > 0) {
this.isorter.column = sorter.field
this.isorter.order = 'ascend' == sorter.order ? 'asc' : 'desc'
}
this.ipagination = pagination
this.loadData()
},
handleAdd: function () {
this.$refs.modalForm.add()
this.$refs.modalForm.title = '新增'
this.$refs.modalForm.disableSubmit = false
},
handleEdit: function (record) {
this.$refs.modalForm.edit(record)
this.$refs.modalForm.title = '编辑'
this.$refs.modalForm.disableSubmit = false
},
handleDetail(record) {
this.handleEdit(record)
this.$refs.modalForm.title = '查看详情'
this.$refs.modalForm.disableSubmit = true
},
modalFormOk() {
this.loadData()
},
handleDelete: function (id) {
if (!this.url.delete) {
this.$message.error('请设置url.delete属性!')
return
}
var that = this
httpAction(that.url.delete, id, 'delete').then((res) => {
if (res.data.success) {
that.$message.success('删除成功' || res.data.msg)
that.loadData()
} else {
that.$message.warning(res.data.msg)
}
})
},
handleToggleSearch() {
this.toggleSearchStatus = !this.toggleSearchStatus
},
getRowClassName(record, index) {
let className = ''
className = index % 2 === 0 ? 'evenRow' : 'oddRow'
return className
},
handleExportXls2() {
let paramsStr = encodeURI(JSON.stringify(this.getQueryParams()))
let url = `${system.devApi}/${this.url.exportXlsUrl}?paramsStr=${paramsStr}`
window.location.href = url
},
handleDownTemplate() {
window.location.href = this.url.downTemplateUrl
},
handleExportXls(fileName) {
if (!fileName || typeof fileName != 'string') {
fileName = '导出文件'
}
let { limit, pageNo, ...param } = this.getQueryParams()
console.log('导出参数', param)
downFile(this.url.exportXlsUrl, param).then((res) => {
let data = res.data
if (!data) {
this.$message.warning('文件下载失败')
return
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xlsx')
} else {
let url = window.URL.createObjectURL(new Blob([data]))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName + '.xlsx')
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
window.URL.revokeObjectURL(url)
}
})
},
handleImportExcel(info) {
this.importLoading = true
if (info.file.status !== 'uploading') {
console.log(info.file, info.fileList)
}
if (info.file.status === 'done') {
this.importLoading = false
let response = info.file.response
if (response.success) {
if (response.code !== 0) {
let {
message,
result: { msg, fileUrl, fileName }
} = info.file.response
let href = system.devApi + fileUrl
this.$message.info({
title: message,
content: (
<div>
<span>{msg}</span>
<br />
<span>
具体详情请{' '}
<a href={href} target="_blank" download={fileName}>
点击下载
</a>{' '}
</span>
</div>
)
})
} else {
this.$message.success(info.file.response.msg || `${info.file.name} 文件上传成功`)
this.loadData()
}
} else {
this.$message.error(info.file.response.msg || '导入模板错误!')
}
} else if (info.file.status === 'error') {
this.$message.error(`文件上传失败: ${info.file.msg} `)
this.importLoading = false
}
},
handleTableHeight() {
let tableOffsetTop = this.$refs['table']?.$el?.getBoundingClientRect().top || 180
this.screenHeight = document.body.clientHeight
this.scrollY = this.screenHeight - tableOffsetTop - 76 - (this.tableTitleColumns || 1) * 40
},
doc(record) {
}
}
}