因为当前行的某些状态或者操作权限,每一行的操作按钮可能展示的数量不同。
所以我们需要找出当前table页,按钮最多的那一行row,并计算出这个row中操作栏的所需宽度,用这个宽度来设置最后一列操作栏的宽度
(按钮分为文字和图标,所以有两版)
columnWidth.js 【按钮是文字版】(当按钮是文字时,用这个)
export default {
data() {
return {
minWidth: 100
}
},
watch: {
minWidth() {
this.setWidth()
}
},
methods: {
setWidth() {
if (this.headerData) {
this.$set(this.headerData[this.headerData.length - 1], 'width', this.minWidth)
}
},
// 获取列表数据后,调用此方法
getTextWidth(ref) {
const trArr = []
const operateTd = []
const buttonNumArr = []
let isContainMore = false // 是否含有'更多'按钮,因为‘更多’后面有个尖括号
this.$nextTick(() => {
// 获取当前table的所有tr
let tr = []
if (ref && this.$refs[ref]) {
tr = this.$refs[ref].$el.querySelectorAll('tbody tr')
} else {
tr = document.querySelectorAll('tbody tr')
}
// console.log(tr)
if (tr.length) {
tr.forEach((item, index) => {
const t = trArr.find(t => t.rowIndex === item.rowIndex)
if (!t) trArr.push(item)
})
}
// 获取每一行tr中操作栏的按钮数量(操作栏在最后一列)
trArr.forEach(item => {
const span = document.createElement('span')
span.classList.add('getColumnWidth')
const td = item.querySelector('td:last-child .cell')
const allSpan = td.querySelectorAll('span')
const textArr = []// 存放每一行的按钮文字
// console.log(btns.length)
allSpan.forEach(b => {
if (b.innerHTML) {
const str = b.innerHTML + ''
const text = str.replace(/<[^>]+>/g, '')
if (text.indexOf('更多') !== -1) {
isContainMore = true
}
if (textArr.inde