// 默认接受四个值 { 当前行的值, 当前列的值, 行的下标, 列的下标 }
const objectSpanMethod = ({
row,
column,
rowIndex, // 需要合并的开始行
columnIndex, // 需要合并的列
}) => {
if (columnIndex === 0 || columnIndex === 1) {
const currentValue = row[column.property];
// 获取上一行相同列的值
const preRow = tableData.value[rowIndex - 1];
const preValue = preRow ? preRow[column.property] : null;
if (currentValue === preValue) {
return { rowspan: 0, colspan: 0 };
} else {
// 否则计算当前单元格应该跨越多少行
let rowspan = 1;
for (let i = rowIndex + 1; i < tableData.value.length; i++) {
const nextRow = tableData.value[i];
const nextValue = nextRow[column.property];
if (nextValue === currentValue) {
rowspan++;
} else {
break;
}
}
return { rowspan, colspan: 1 };
}
}
if (columnIndex === 2 || columnIndex === 3) {
const current = row.userId;
const preRowten = tableData.value[rowIndex - 1] ? tableData.value[rowIndex - 1].userId : '';
const currentValue = row[column.property];
const preRow = tableData.value[rowIndex - 1];
const preValue = preRow ? preRow[column.property] : null;
if (current === preRowten && currentValue === preValue) {
return { rowspan: 0, colspan: 0 };
} else {
let rowspan = 1;
for (let i = rowIndex + 1; i < tableData.value.length; i++) {
const nextRow = tableData.value[i];
const nextValue = nextRow.userId
const nextWork = tableData.value[i];
const nextValueWork = nextWork[column.property];
if (nextValue === current && nextValueWork == currentValue) {
rowspan++;
} else {
break;
}
}
return { rowspan, colspan: 1 };
}
}
if(columnIndex === 4) {
console.log(row,'columnIndex');
const current = row.workDate;
const preRowten = tableData.value[rowIndex - 1] ? tableData.value[rowIndex - 1].workDate : '';
const currentValue = row[column.property];
const preRow = tableData.value[rowIndex - 1];
const preValue = preRow ? preRow[column.property] : null;
if (current === preRowten && currentValue === preValue) {
return { rowspan: 0, colspan: 0 };
} else {
let rowspan = 1;
for (let i = rowIndex + 1; i < tableData.value.length; i++) {
const nextRow = tableData.value[i];
const nextValue = nextRow.workDate
const nextWork = tableData.value[i];
const nextValueWork = nextWork[column.property];
if (nextValue === current && nextValueWork == currentValue) {
rowspan++;
} else {
break;
}
}
return { rowspan, colspan: 1 };
}
}
}
el-table 中 :span-method=“objectSpanMethod“合并单元格
于 2024-01-26 15:10:01 首次发布