1. vue2
对自己公司业务代码进行了一些封装,不太能保证其他人能够通用。
<template>
<div class="base-table">
<el-table
ref="tableRef"
:style="{ width: '100%' }"
:data="tableData"
:height="mannualHeight ? mannualHeight : tableHeight"
:max-height="maxHeight"
:border="border"
:header-cell-style="{ 'text-align': align }"
:cell-style="{ 'text-align': align }"
@selection-change="changeTableSelection"
@row-click="clickSingleRow"
>
<!-- expand -->
<el-table-column v-if="isExpanded" type="expand">
<template slot-scope="scope">
<slot name="expand" :scope="scope" />
</template>
</el-table-column>
<!-- select box -->
<el-table-column v-if="selected" type="selection" width="50" :align="align" />
<!-- index -->
<el-table-column type="index" label="序号" width="70" :align="align" />
<template v-for="(column, index) in tableColumns">
<!-- normal table column -->
<el-table-column
v-if="isCommonTableColumn(column) && !column.hidden"
:key="index + +column.attrs.label"
v-bind="column.attrs || {}"
/>
<el-table-column v-else-if="!column.hidden" :key="index + column.attrs.label">
<template v-slot="scope">
<span v-if="column.slot">
<slot :name="column.slot" :scope="scope" />
</span>
</template>
</el-table-column>
</template>
</el-table>
<el-pagination
v-if="pager"
:current-page="requestParams.pageNum"
:page-sizes="customPageSizes"
:page-size="requestParams.pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="total"
@size-change="changeTableSize"
@current-change="changeTablePageNum"
></el-pagination>
</div>
</template>
<script>
export default {
name: 'BaseTable',
props: {
// table
isExpanded: {
type: Boolean,
default: false
},
selected: {
type: Boolean,
default: false
},
disabledSelectionLabels: {
type: Array,
default: () => ['操作']
},
tableData: {
type: Array,
default: () => []
},
tableColumns: {
type: Array,
default: () => []
},
mannualHeight: {
type: Number,
default: 0
},
updatedHeight: {
type: Number,
default: 0
},
maxHeight: {
type: Number,
default: 0
},
border: {
type: Boolean,
default: true
},
stripe: {
type: Boolean,
default: true
},
align: {
type: String,
default: 'center'
},
// pagination
pager: {
type: Boolean,
default: true
},
requestParams: {
type: Object,
default: () => ({
pageNum: 1,
pageSize: 20
})
},
customPageSizes: {
type: Array,
default: () => [20, 50, 100]
},
total: {
type: Number,
default: 0
},
background: {
type: Boolean,
default: false
}
},
data() {
return {
tableRef: null,
tableHeight: 670
}
},
methods: {
// if it has one of the following attribute, then it isn't a normal table column.
isCommonTableColumn(column) {
const specialColumnList = ['slot']
return !specialColumnList.some(option => column[option])
},
clickSingleRow(row, column) {
const isFound = this.disabledSelectionLabels.find(item => item.label === column.label)
if (!isFound) {
this.$refs.tableRef.toggleRowSelection(row)
}
},
changeTableSelection(val) {
this.$emits('updateTableSelection', val)
},
changeTablePageNum(pageNum) {
const updatedParams = { ...this.requestParams, pageNum: pageNum }
this.$emits('updateRequestParams', updatedParams)
},
changeTableSize(pageSize) {
const updatedParams = { ...this.requestParams, pageSize: pageSize }
this.$emits('updateRequestParams', updatedParams)
},
// touch bottom to load
// Pending
loadWithScrollingToBottom() {
const wrapRef = this.$refs.tableRef.scrollBarRef.wrapRef
if (!wrapRef) return
const { scrollHeight, scrollTop, clientHeight } = wrapRef
if (scrollHeight - clientHeight === scrollTop) {
this.$emits('updateLoadingMore')
}
}
},
activated() {
this.$nextTick(function() {
this.tableHeight = window.innerHeight - this.updatedHeight
if (this.tableHeight < 100) {
this.tableHeight = 100
}
// 监听窗口大小变化
let self = this
window.onresize = function() {
self.tableHeight = window.innerHeight - this.updatedHeight
if (self.tableHeight < 100) {
self.tableHeight = 100
}
}
})
}
}
</script>
<style lang="less" scoped></style>