el-table 中 :span-method=“objectSpanMethod“合并单元格

// 默认接受四个值 { 当前行的值, 当前列的值, 行的下标, 列的下标 }
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 };
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值