使用 `span-method` 合并 el-table 表格时,为什么第四列无法合并?
学习文章要努力,但是不要急!今天的这篇文章《使用 `span-method` 合并 el-table 表格时,为什么第四列无法合并? 》将会介绍到等等知识点,如果你想深入学习文章,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

el-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 表格时,为什么第四列无法合并? 》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~米云公众号也会发布文章相关知识,快来关注吧!
- Win10怎么关闭垃圾弹窗 Win10关闭垃圾弹窗方法
