<template>
<div class="container" ref="tableData">
<m-loading
class="main"
:loading="loading">
<el-table
v-bind="$attrs"
v-on="$listeners"
:row-key="rowKey"
border
style="width: 100%;"
:data="data"
@sort-change="sortChange"
@selection-change="selectionChange"
>
<template v-for="(item) in columns">
<el-table-column
:key="item.prop || item.type"
v-bind="item"
:header-align="item.headerAlign || 'center'"
show-overflow-tooltip>
<template v-if="item.slotName" v-slot="scope">
<slot :name="item.slotName" :data="scope"></slot>
</template>
</el-table-column>
</template>
</el-table>
<template v-if="data.length">
<el-pagination
v-if="hasPagination"
class="pagination"
background
:layout="layout"
:page-size.sync="pagination.limit"
:total="pagination.total"
:current-page.sync="pagination.page"
@size-change="sizeChange"
@current-change="currentChange"
/>
</template>
</m-loading>
</div>
</template>
<script>
export default {
name: 'index',
props: {
loading: {
type: Boolean,
default: false
},
rowKey: {
type: String,
default: 'id'
},
columns: {
type: Array,
default: () => []
},
pagination: {
type: Object,
default: () => {}
},
data: {
type: Array,
default: () => []
}
},
data() {
return {
height: 0
}
},
watch: {
},
computed: {
hasPagination() {
return !!this.pagination;
},
layout() {
const layout = 'total, sizes, prev, pager, next, jumper';
return this.pagination?.layout || layout;
},
},
methods: {
sizeChange(limit) {
this.pagination.page = 1;
this.pagination.limit = limit;
this.$emit('pagination-change');
},
currentChange(page) {
this.pagination.page = page;
this.$emit('pagination-change');
},
sortChange({ prop, order }) {
const sortObj = {
sort: '',
order: ''
}
if (order) {
sortObj.sort = prop;
sortObj.order = order === 'ascending' ? 'asc' : 'desc';
}
this.$emit('sortChange', sortObj)
},
selectionChange(val) {
this.$emit('selectionChange', val);
}
},
mounted() {
this.height = this.$refs.tableData.offsetHeight;
}
}
</script>
<style lang="scss" scoped>
.container {
width: 100%;
flex: 1;
}
.main {
background: #fff;
padding: 15px;
}
.pagination {
text-align: right;
margin-top: 20px;
}
</style>