初始版本EleTableMixin
export const EleTableMixin = {
data() {
return {
tableData: [],
tableMaxHeight: 400,
total: 0
}
},
computed: {
options() {
return {
data: this.tableData,
maxHeight: this.tableMaxHeight
}
}
},
mounted() {
window.addEventListener('resize', () => {
this.setTableHeight()
})
},
methods: {
getTableHeight() {
const AppMain = document.querySelector('.AppMain')
const searchForm = document.querySelector('.searchForm')
const tableTopBtns = document.querySelector('.ele-table-button-group')
const tablePagination = document.querySelector('.ele-table-pagination')
const AppMainHeight = (AppMain && AppMain.offsetHeight) || 0
const searchFormHeight = (searchForm && searchForm.offsetHeight) || 0
const searchFormMarginBottom =
(searchForm && document.defaultView.getComputedStyle(searchForm).marginBottom) || 0
const tableTopBtnsHeight = (tableTopBtns && tableTopBtns.offsetHeight) || 0
const tablePaginationHeight = (tablePagination && tablePagination.offsetHeight) || 0
return (
AppMainHeight -
searchFormHeight -
searchFormMarginBottom.slice(0, -2) * 1 -
tableTopBtnsHeight -
tablePaginationHeight
)
},
setTableHeight() {
this.$nextTick(() => {
this.tableMaxHeight = this.getTableHeight()
})
},
indexMethod(index) {
return (this.searchForm.page - 1) * this.searchForm.rows + index + 1
}
}
}
页面有其他部分的高度需要减去 - 优化
export const EleTableMixin = {
data() {
return {
tableData: [],
tableMaxHeight: 400,
total: 0,
otherHeight: 0
}
},
computed: {
options() {
return {
data: this.tableData,
maxHeight: this.tableMaxHeight
}
}
},
mounted() {
window.addEventListener('resize', () => {
this.setTableHeight(this.otherHeight)
})
},
methods: {
getTableHeight(otherHeight = 0) {
console.log('otherHeight----', otherHeight)
const AppMain = document.querySelector('.AppMain')
const searchForm = document.querySelector('.searchForm')
const tableTopBtns = document.querySelector('.ele-table-button-group')
const tablePagination = document.querySelector('.ele-table-pagination')
const AppMainHeight = (AppMain && AppMain.offsetHeight) || 0
const searchFormHeight = (searchForm && searchForm.offsetHeight) || 0
const tableTopBtnsHeight = (tableTopBtns && tableTopBtns.offsetHeight) || 0
const tablePaginationHeight = (tablePagination && tablePagination.offsetHeight) || 0
return (
AppMainHeight - searchFormHeight - tableTopBtnsHeight - tablePaginationHeight - otherHeight
)
},
setTableHeight(otherHeight = 0) {
this.otherHeight = otherHeight
this.$nextTick(() => {
this.tableMaxHeight = this.getTableHeight(otherHeight)
})
},
indexMethod(index) {
return (this.searchForm.page - 1) * this.searchForm.rows + index + 1
}
}
}
图片展示
优化 - 其他容器用类名数组传递遍历计算
export const EleTableMixin = {
data() {
return {
tableData: [],
tableMaxHeight: 400,
total: 0,
fatherClass: '',
sonClassList: '',
otherHeight: 0
}
},
computed: {
options() {
return {
data: this.tableData,
maxHeight: this.tableMaxHeight
}
}
},
mounted() {
window.addEventListener('resize', () => {
this.setTableHeight(this.fatherClass, this.sonClassList, this.otherHeight)
})
},
methods: {
getTableHeight(fatherClass, sonClassList, otherHeight) {
const fatherDom = document.querySelector(fatherClass)
const fatherHeight = (fatherDom && fatherDom.offsetHeight) || 0
let sonHeighSum = 0
sonClassList.forEach((sonClass) => {
const sonDom = document.querySelector(sonClass)
const sonHeight = (sonDom && sonDom.offsetHeight) || 0
console.log('sonClass----', sonClass)
console.log('sonHeight----', sonHeight)
sonHeighSum += sonHeight
})
console.log('fatherHeight----', fatherHeight)
console.log('sonHeighSum----', sonHeighSum)
return fatherHeight - sonHeighSum - otherHeight
},
setTableHeight(
fatherClass = '.app-main',
sonClassList = ['.searchForm', '.ele-table-button-group', '.ele-table-pagination'],
otherHeight = 20
) {
this.sonClassList = sonClassList
this.fatherClass = fatherClass
this.otherHeight = otherHeight
this.$nextTick(() => {
this.tableMaxHeight = this.getTableHeight(fatherClass, sonClassList, otherHeight)
})
},
indexMethod(index, { page, rows }) {
return (page - 1) * rows + index + 1
}
}
}