单元格横向、纵向合并Vue

//页面table 加入方法
<el-table :span-method="objectSpanMethod">

// 将获取的数据进行合并
this.getMergeObj(this.tableData);
/**
         * 获取合并对象
         * @param {Object} list 列表
         */
        getMergeObj (list) {
            this.mergeObj = {}
            // 标记
            let mack = 0
            for (let key in list[0]) {
                this.mergeObj[key] = []
                mack = 0
                list.forEach((item, index) => {
                    if (index === 0) {
                        this.mergeObj[key].push(1)
                    } else {
                        if (item[key] === list[index - 1][key] && item[key]) {
                            this.mergeObj[key][mack] += 1
                            this.mergeObj[key].push(0)
                        } else {
                            mack = index
                            this.mergeObj[key].push(1)
                        }
                    }
                })
            }
            console.log('-合并对象:', this.mergeObj)
        },
        // 单元格合并 纵向及横向合并 lable = ‘targetName / targetChildren’
        objectSpanMethod({ row, column, rowIndex, columnIndex }) {
            let col = 1;
            if(row.targetName == row.targetChildren && column.property === 'targetName'){
                col = 2;
            }
            if (row.targetName == row.targetChildren && column.property === 'targetChildren'){
                col = 0;
            }
            // 需要合并的行
            if (column.property === 'targetName' || column.property === 'targetChildren') {
                return {
                    rowspan: this.mergeObj[column.property][rowIndex],
                    colspan: col
                }
            } else {
                return { rowspan: 1, colspan: 1 }
            }
            //row:行对象,带有一行所有数据,column:列对象
            //rowIndex:行索引,columnIndex:列索引
            //加载表格时从(0,0)开始,(0,1)···(1,0),(1,1)···依次获取数据
            if(columnIndex===2){
                //每次走到第3列时给单元格加上rowspan和colspan属性
                const _row = this.tableData[rowIndex];//从处理完的数组里获取
                const _col = _row>0?1:0;
                return {
                    rowspan:_row,
                    colspan:_col //相当于给给表格加上rowspan,colspan属性
                }
            }
        },

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值