x-php-Admin/src/components/xTable/index.vue

413 lines
13 KiB
Vue
Raw Normal View History

2023-06-07 10:48:30 +00:00
<template>
2023-07-10 16:41:24 +00:00
<div class="xTable" :style="{'height':_height}" ref="xTableMain" v-loading="loading">
<div class="xTable-table" :style="{'height':_table_height}">
<el-table v-bind="$attrs" :data="tableData" :row-key="rowKey" :key="toggleIndex" ref="xTable" :height="height=='auto'?null:'100%'" :size="config.size" :border="config.border" :stripe="config.stripe" :summary-method="remoteSummary?remoteSummaryMethod:summaryMethod" @sort-change="sortChange" @filter-change="filterChange">
2023-06-07 10:48:30 +00:00
<slot></slot>
2023-06-19 14:55:32 +00:00
<template v-for="(item, index) in column" :key="index">
2023-06-26 11:06:05 +00:00
2023-07-13 00:44:51 +00:00
<el-table-column :align="item.align || 'left'" :label="item.label" v-if="item.column && item.column.length>0">
<el-table-column v-for="(items, indexs) in item.column" :key="indexs" :align="items.align || 'left'" :column-key="items.prop||items.name" :label="items.label" :prop="items.prop||items.name" :width="items.width || 'auto'" :min-width="items.minWidth || 'auto'" :sortable="items.sortable" :fixed="items.fixed" :filters="items.filters" :filter-method="remoteFilter||!items.filters?null:filterHandler" :show-overflow-tooltip="items.showOverflowTooltip">
<template #default="scope">
<columnItem v-model="scope.row" :item="items"> </columnItem>
</template>
</el-table-column>
</el-table-column>
2023-07-13 10:54:21 +00:00
<el-table-column v-else-if="!item.hide && (item.name || item.prop)" :align="item.align || 'left'" :column-key="item.prop||item.name" :label="item.label" :prop="item.prop||item.name" :width="item.width || 'auto'" :min-width="item.minWidth || 'auto'" :sortable="item.sortable" :fixed="item.fixed" :filters="item.filters" :filter-method="remoteFilter||!item.filters?null:filterHandler" :show-overflow-tooltip="item.showOverflowTooltip">
2023-07-13 00:44:51 +00:00
<template #default="scope">
<columnItem v-model="scope.row" :item="item"> </columnItem>
2023-06-07 10:48:30 +00:00
</template>
</el-table-column>
</template>
2023-07-13 00:44:51 +00:00
2023-06-07 10:48:30 +00:00
<el-table-column min-width="1"></el-table-column>
<template #empty>
<el-empty :description="emptyText" :image-size="100"></el-empty>
</template>
</el-table>
</div>
2023-07-10 16:41:24 +00:00
<div class="xTable-page" v-if="!hidePagination || !hideDo">
<div class="xTable-pagination">
2023-06-07 10:48:30 +00:00
<el-pagination v-if="!hidePagination" background :small="true" :layout="paginationLayout" :total="total" :page-size="scPageSize" :page-sizes="pageSizes" v-model:currentPage="currentPage" @current-change="paginationChange" @update:page-size="pageSizeChange"></el-pagination>
</div>
2023-07-10 16:41:24 +00:00
<div class="xTable-do" v-if="!hideDo">
2023-06-07 10:48:30 +00:00
<el-button v-if="!hideRefresh" @click="refresh" icon="el-icon-refresh" circle style="margin-left:15px"></el-button>
2023-06-19 14:55:32 +00:00
<el-popover v-if="columnSetting" placement="top" title="列设置" :width="500" trigger="click" :hide-after="0" @show="customColumnShow=true" @after-leave="customColumnShow=false">
2023-06-07 10:48:30 +00:00
<template #reference>
<el-button icon="el-icon-set-up" circle style="margin-left:15px"></el-button>
</template>
2023-06-19 14:55:32 +00:00
<columnSetting v-if="customColumnShow" ref="columnSetting" @userChange="columnSettingChange" @save="columnSettingSave" @back="columnSettingBack" :column="column"></columnSetting>
2023-06-07 10:48:30 +00:00
</el-popover>
<el-popover v-if="!hideSetting" placement="top" title="表格设置" :width="400" trigger="click" :hide-after="0">
<template #reference>
<el-button icon="el-icon-setting" circle style="margin-left:15px"></el-button>
</template>
<el-form label-width="80px" label-position="left">
<el-form-item label="表格尺寸">
<el-radio-group v-model="config.size" size="small" @change="configSizeChange">
<el-radio-button label="large"></el-radio-button>
<el-radio-button label="default">正常</el-radio-button>
<el-radio-button label="small"></el-radio-button>
</el-radio-group>
</el-form-item>
<el-form-item label="样式">
<el-checkbox v-model="config.border" label="纵向边框"></el-checkbox>
<el-checkbox v-model="config.stripe" label="斑马纹"></el-checkbox>
</el-form-item>
</el-form>
</el-popover>
</div>
</div>
</div>
</template>
2023-07-05 14:34:51 +00:00
<style scoped>
2023-07-10 16:41:24 +00:00
.xTable {}
.xTable-table {height: calc(100% - 50px);}
.xTable-page {height:50px;display: flex;align-items: center;justify-content: space-between;padding:0 15px;}
.xTable-do {white-space: nowrap;}
.xTable:deep(.el-table__footer) .cell {font-weight: bold;}
.xTable:deep(.el-table__body-wrapper) .el-scrollbar__bar.is-horizontal {height: 12px;border-radius: 12px;}
.xTable:deep(.el-table__body-wrapper) .el-scrollbar__bar.is-vertical {width: 12px;border-radius: 12px;}
2023-07-05 14:34:51 +00:00
</style>
2023-06-07 10:48:30 +00:00
<script>
import config from "@/config/table";
import columnSetting from './columnSetting'
2023-07-13 00:44:51 +00:00
import columnItem from './columnItem'
2023-06-07 10:48:30 +00:00
export default {
2023-07-10 16:41:24 +00:00
name: 'xTable',
2023-06-07 10:48:30 +00:00
components: {
2023-07-13 00:44:51 +00:00
columnSetting,
columnItem,
2023-06-07 10:48:30 +00:00
},
props: {
2023-06-19 14:55:32 +00:00
name: { type: String, default: "" },
tableColumn: { type: Object, default: () => {} },
2023-06-20 11:03:48 +00:00
api: { type: Object, default: () => {} },
2023-06-07 10:48:30 +00:00
apiObj: { type: Object, default: () => {} },
params: { type: Object, default: () => ({}) },
data: { type: Object, default: () => {} },
height: { type: [String,Number], default: "100%" },
size: { type: String, default: "default" },
border: { type: Boolean, default: false },
stripe: { type: Boolean, default: false },
2023-06-26 11:06:05 +00:00
columnSetting: { type: Boolean, default: false },
2023-06-07 10:48:30 +00:00
pageSize: { type: Number, default: config.pageSize },
pageSizes: { type: Array, default: config.pageSizes },
rowKey: { type: String, default: "" },
summaryMethod: { type: Function, default: null },
remoteSort: { type: Boolean, default: false },
remoteFilter: { type: Boolean, default: false },
remoteSummary: { type: Boolean, default: false },
hidePagination: { type: Boolean, default: false },
hideDo: { type: Boolean, default: false },
hideRefresh: { type: Boolean, default: false },
hideSetting: { type: Boolean, default: false },
paginationLayout: { type: String, default: config.paginationLayout },
},
watch: {
2023-06-19 14:55:32 +00:00
2023-06-07 10:48:30 +00:00
//监听从props里拿到值了
data(){
this.tableData = this.data;
this.total = this.tableData.length;
},
apiObj(){
this.tableParams = this.params;
this.refresh();
},
2023-06-20 11:03:48 +00:00
api(){
this.tableParams = this.params;
this.refresh();
},
2023-06-19 14:55:32 +00:00
tableColumn(){
this.column = this.tableColumn;
2023-06-07 10:48:30 +00:00
}
},
computed: {
_height() {
return Number(this.height)?Number(this.height)+'px':this.height
},
_table_height() {
return this.hidePagination && this.hideDo ? "100%" : "calc(100% - 50px)"
}
},
data() {
return {
scPageSize: this.pageSize,
isActivat: true,
emptyText: "暂无数据",
toggleIndex: 0,
tableData: [],
total: 0,
currentPage: 1,
prop: null,
order: null,
loading: false,
2023-07-05 14:34:51 +00:00
visible: false,
2023-06-07 10:48:30 +00:00
tableHeight:'100%',
tableParams: this.params,
2023-06-19 14:55:32 +00:00
column: [],
2023-06-07 10:48:30 +00:00
customColumnShow: false,
summary: {},
2023-07-05 14:34:51 +00:00
visibleInfo: {},
2023-06-07 10:48:30 +00:00
config: {
size: this.size,
border: this.border,
stripe: this.stripe
2023-06-20 11:03:48 +00:00
},
2023-06-07 10:48:30 +00:00
}
},
mounted() {
2023-06-20 11:03:48 +00:00
this.column = this.tableColumn
2023-06-07 10:48:30 +00:00
//判断是否静态数据
2023-06-20 11:03:48 +00:00
if(this.apiObj || this.api){
2023-06-07 10:48:30 +00:00
this.getData();
}else if(this.data){
this.tableData = this.data;
this.total = this.tableData.length
}
},
activated(){
if(!this.isActivat){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.doLayout()
2023-06-07 10:48:30 +00:00
}
},
2023-06-18 15:42:02 +00:00
deactivated(){
2023-06-07 10:48:30 +00:00
this.isActivat = false;
},
methods: {
//获取数据
async getData(){
this.loading = true;
var reqData = {
[config.request.page]: this.currentPage,
[config.request.pageSize]: this.scPageSize,
[config.request.prop]: this.prop,
[config.request.order]: this.order
}
if(this.hidePagination){
delete reqData[config.request.page]
delete reqData[config.request.pageSize]
}
Object.assign(reqData, this.tableParams)
try {
2023-06-20 11:03:48 +00:00
var xawait = this.api ? this.$http.get(this.api, reqData) : this.apiObj(reqData);
xawait.then((res) => {
try {
var response = config.parseData(res);
}catch(error){
this.loading = false;
this.emptyText = "数据格式错误";
return false;
}
if(response.code != config.successCode){
this.loading = false;
this.emptyText = response.message;
}else{
this.emptyText = "暂无数据";
2023-06-26 11:06:05 +00:00
this.tableData = (this.hidePagination? response.data : response.rows) || [];
2023-06-20 11:03:48 +00:00
this.total = response.total || 0;
this.summary = response.summary || {};
this.loading = false;
}
2023-06-19 14:55:32 +00:00
2023-07-10 16:41:24 +00:00
// this.$refs.xTable.setScrollTop(0)
2023-06-20 11:03:48 +00:00
// 回调
this.$emit('dataChange', res, this.tableData)
});
2023-06-19 14:55:32 +00:00
2023-06-07 10:48:30 +00:00
}catch(error){
this.loading = false;
this.emptyText = error.statusText;
return false;
}
2023-06-20 11:03:48 +00:00
2023-06-07 10:48:30 +00:00
},
//分页点击
paginationChange(){
this.getData();
},
//条数变化
pageSizeChange(size){
this.scPageSize = size
this.getData();
},
//刷新数据
refresh(){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.clearSelection();
2023-06-07 10:48:30 +00:00
this.getData();
},
//更新数据 合并上一次params
upData(params, page=1){
this.currentPage = page;
2023-07-10 16:41:24 +00:00
this.$refs.xTable.clearSelection();
2023-06-07 10:48:30 +00:00
Object.assign(this.tableParams, params || {})
this.getData()
},
//重载数据 替换params
reload(params, page=1){
this.currentPage = page;
this.tableParams = params || {}
2023-07-10 16:41:24 +00:00
this.$refs.xTable.clearSelection();
this.$refs.xTable.clearSort()
this.$refs.xTable.clearFilter()
2023-06-07 10:48:30 +00:00
this.getData()
},
//自定义变化事件
2023-06-19 14:55:32 +00:00
columnSettingChange(column){
this.column = column;
2023-06-07 10:48:30 +00:00
this.toggleIndex += 1;
},
//自定义列保存
2023-06-19 14:55:32 +00:00
async columnSettingSave(column){
2023-06-07 10:48:30 +00:00
this.$refs.columnSetting.isSave = true
try {
2023-06-19 14:55:32 +00:00
await config.columnSettingSave(this.name, column)
2023-06-07 10:48:30 +00:00
}catch(error){
this.$message.error('保存失败')
this.$refs.columnSetting.isSave = false
}
this.$message.success('保存成功')
this.$refs.columnSetting.isSave = false
},
//自定义列重置
async columnSettingBack(){
this.$refs.columnSetting.isSave = true
try {
2023-06-19 14:55:32 +00:00
const column = await config.columnSettingReset(this.name, this.column)
this.column = column
this.$refs.columnSetting.column = JSON.parse(JSON.stringify(this.column||[]))
2023-06-07 10:48:30 +00:00
}catch(error){
this.$message.error('重置失败')
this.$refs.columnSetting.isSave = false
}
this.$refs.columnSetting.isSave = false
},
//排序事件
sortChange(obj){
if(!this.remoteSort){
return false
}
2023-06-19 14:55:32 +00:00
if(obj.tableColumn && obj.prop){
2023-06-07 10:48:30 +00:00
this.prop = obj.prop
this.order = obj.order
}else{
this.prop = null
this.order = null
}
this.getData()
},
//本地过滤
filterHandler(value, row, column){
const property = column.property;
return row[property] === value;
},
//过滤事件
filterChange(filters){
if(!this.remoteFilter){
return false
}
Object.keys(filters).forEach(key => {
filters[key] = filters[key].join(',')
})
this.upData(filters)
},
//远程合计行处理
remoteSummaryMethod(param){
const {columns} = param
const sums = []
columns.forEach((column, index) => {
if(index === 0) {
sums[index] = '合计'
return
}
const values = this.summary[column.property]
if(values){
sums[index] = values
}else{
sums[index] = ''
}
})
return sums
},
configSizeChange(){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.doLayout()
2023-06-07 10:48:30 +00:00
},
//插入行 unshiftRow
unshiftRow(row){
this.tableData.unshift(row)
},
//插入行 pushRow
pushRow(row){
this.tableData.push(row)
},
//根据key覆盖数据
updateKey(row, rowKey=this.rowKey){
this.tableData.filter(item => item[rowKey]===row[rowKey] ).forEach(item => {
Object.assign(item, row)
})
},
//根据index覆盖数据
updateIndex(row, index){
Object.assign(this.tableData[index], row)
},
//根据index删除
removeIndex(index){
this.tableData.splice(index, 1)
},
//根据index批量删除
removeIndexes(indexes=[]){
indexes.forEach(index => {
this.tableData.splice(index, 1)
})
},
//根据key删除
removeKey(key, rowKey=this.rowKey){
this.tableData.splice(this.tableData.findIndex(item => item[rowKey]===key), 1)
},
//根据keys批量删除
removeKeys(keys=[], rowKey=this.rowKey){
keys.forEach(key => {
this.tableData.splice(this.tableData.findIndex(item => item[rowKey]===key), 1)
})
},
//原生方法转发
clearSelection(){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.clearSelection()
2023-06-07 10:48:30 +00:00
},
toggleRowSelection(row, selected){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.toggleRowSelection(row, selected)
2023-06-07 10:48:30 +00:00
},
toggleAllSelection(){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.toggleAllSelection()
2023-06-07 10:48:30 +00:00
},
toggleRowExpansion(row, expanded){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.toggleRowExpansion(row, expanded)
2023-06-07 10:48:30 +00:00
},
setCurrentRow(row){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.setCurrentRow(row)
2023-06-07 10:48:30 +00:00
},
clearSort(){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.clearSort()
2023-06-07 10:48:30 +00:00
},
clearFilter(columnKey){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.clearFilter(columnKey)
2023-06-07 10:48:30 +00:00
},
doLayout(){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.doLayout()
2023-06-07 10:48:30 +00:00
},
sort(prop, order){
2023-07-10 16:41:24 +00:00
this.$refs.xTable.sort(prop, order)
2023-06-07 10:48:30 +00:00
}
}
}
</script>