element-ui table组件根据后端返回数据结构实现自定义单元格合并

element-ui table 组件自定义单元格合并

问题: 当后端接口返回的数据是带层级的结构,我们要怎么实现自定义的单元格合并功能呢?

// 服务端接口返回值如下
const mockData = [
        {
          title1: '我是title1',
          children: [
              {
                title2: '我是title2',
                children: [
                  {
                    title3: '我是title3',
                    title4: '我是title4',
                    title5: '我是title5',
                    title6: '我是title6'
                  },
                  {
                    title3: '我是title3',
                    title4: '我是title4',
                    title5: '我是title5',
                    title6: '我是title6'
                  }
                ]
              },
              {
                title2: '我是title2',
                children: [
                  {
                    title3: '我是title3',
                    title4: '我是title4',
                    title5: '我是title5',
                    title6: '我是title6'
                  }
                ]
              }
          ]
        },
        {
          title1: '我是title1的兄弟',
          children: [
            {
              title2: '我是title2的兄弟',
              children: [
                {
                  title3: '我是title3的兄弟',
                  title4: '我是title4的兄弟',
                  title5: '我是title5的兄弟',
                  title6: '我是title6的兄弟'
                },
                {
                  title3: '我是title3的兄弟',
                  title4: '我是title4的兄弟',
                  title5: '我是title5的兄弟',
                  title6: '我是title6的兄弟'
                }
              ]
            },
            {
              title2: '我是title2',
              children: [
                {
                  title3: '我是title3',
                  title4: '我是title4',
                  title5: '我是title5',
                  title6: '我是title6'
                }
              ]
            }
          ]
        }
      ] 

这是我们想要的效果

在这里插入图片描述

  1. 首先需要将上面的数据扁平化。
methods: {
  dealData() {
      this.mockData.forEach(item => {
        let arr = []
        this.flat(item, arr)
        this.dataSource = this.dataSource.concat(arr)
      })
    },
    
   // 通过递归将传入的对象扁平化
  flat(obj, arr) {
      if(!obj.children) {
        arr.push(obj)
      } else {
        const children = cloneDeep(obj.children)
        delete obj.children
        children.forEach((item) => {
          this.flat({ ...obj, ...item }, arr)
        })
      }
    },
}
  1. 利用table组件提供的span-method合并方法实现自定义合并
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if(columnIndex === 0 || columnIndex === 1) {
        // 获取当前行的值
        const curRowValue = row[column.property]
        // 获取上一行的值
        const preRow = this.dataSource[rowIndex - 1]
        const preRowValue = preRow ? preRow[column.property] : null

        if(preRowValue === curRowValue) {
          return { rowspan: 0, colspan: 0 }
        } else {
          let rowspan = 1
          for (let i = rowIndex + 1; i < this.dataSource.length; i ++) {
            const nextRowValue = this.dataSource[i][column.property]
            if(nextRowValue === curRowValue) {
              rowspan ++
            } else {
              break
            }
          }
          return { rowspan, colspan: 1 }
        }
      }
    }

tip: 如果说服务端返回的结构本来就是扁平化的数据,那就可以直接使用第2步的方法实现了。

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值