使用 `span-method` 合并 el-table 表格时,为什么第四列无法合并?(合并.表格.method.span.el.....)
wufei123 2025-01-26 阅读:61 评论:0el-table合并部分成功
问题:
使用 span-method 方法合并 el-table 表格的某些列,但发现第四列无法合并。
原因:
原始实现中,第四列的合并判断条件不正确。
解决方案:
使用正确的合并逻辑,具体步骤如下:
- 在 data 中创建三个数组,用于跟踪第一列、第二列和第四列的可合并行数。
- 在 created 生命周期钩子中,调用 merge 方法来计算并填充这些数组。
- 在 span-method 方法中,根据不同的列索引使用不同的逻辑来计算合并跨度。
修改后的代码如下:
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 表格时,为什么第四列无法合并?的详细内容,更多请关注知识资源分享宝库其它相关文章!
版权声明
本站内容来源于互联网搬运,
仅限用于小范围内传播学习,请在下载后24小时内删除,
如果有侵权内容、不妥之处,请第一时间联系我们删除。敬请谅解!
E-mail:dpw1001@163.com
发表评论