
el-table合并部分成功
问题:
使用 span-method 方法合并 el-table 表格的某些列,但发现第四列无法合并。
原因:
原始实现中,第四列的合并判断条件不正确。
解决方案:
使用正确的合并逻辑,具体步骤如下:
修改后的代码如下:
export default {
data() {
return {
waterData: [...],
companyArr: [], // 用于跟踪第一列的可合并行数
companyPos: 0,
simpleArr: [], // 用于跟踪第二列的可合并行数
simplePos: 0,
symbolArr: [], // 用于跟踪第四列的可合并行数
symbolPos: 0,
}
},
created() {
this.merge(this.waterData)
},
methods: {
merge(tableData) {
// 要合并的数组的方法
this.companyArr = [];
this.companyPos = 0;
this.simpleArr = [];
this.simplePos = 0;
this.symbolArr = [];
this.symbolPos = 0;
for (let i = 0; i < tableData.length; i++) {
if (i === 0) {
// 第一行必须存在
this.companyArr.push(1);
this.companyPos = 0;
this.simpleArr.push(1);
this.simplePos = 0;
this.symbolArr.push(1);
this.symbolPos = 0;
} else {
// 第一列
if (tableData[i].name === tableData[i - 1].name) {
this.companyArr[this.companyPos] += 1;
this.companyArr.push(0);
} else {
this.companyArr.push(1);
this.companyPos = i;
}
// 第二列
if (
tableData[i].name === tableData[i - 1].name &&
tableData[i].factor === tableData[i - 1].factor
) {
this.simpleArr[this.simplePos] += 1;
this.simpleArr.push(0);
} else {
this.simpleArr.push(1);
this.simplePos = i;
}
// 第四列
if (
tableData[i].name === tableData[i - 1].name &&
tableData[i].factor === tableData[i - 1].factor &&
tableData[i].symbol === tableData[i - 1].symbol
) {
this.symbolArr[this.symbolPos] += 1;
this.symbolArr.push(0);
} else {
this.symbolArr.push(1);
this.symbolPos = i;
}
}
}
},
arraySpanMethod({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 0) {
// 合并第一列
const _row_1 = this.companyArr[rowIndex];
const _col_1 = _row_1 > 0 ? 1 : 0; // 如果被合并了_row=0则它这个列需要取消
return {
rowspan: _row_1,
colspan: _col_1,
};
} else if (columnIndex === 1) {
// 合并第二列
const _row_2 = this.simpleArr[rowIndex];
const _col_2 = _row_2 > 0 ? 1 : 0;
return {
rowspan: _row_2,
colspan: _col_2,
};
} else if (columnIndex === 3) {
// 合并第四列
const _row_4 = this.symbolArr[rowIndex];
const _col_4 = _row_4 > 0 ? 1 : 0;
return {
rowspan: _row_4,
colspan: _col_4,
};
} else {
return {
rowspan: 1,
colspan: 1,
};
}
},
},
};这样,就可以正确合并 el-table 表格的前一列、第二列和第四列。
以上就是使用 `span-method` 合并 el-table 表格时,为什么第四列无法合并?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号