elementUI合并列

  • Post author:
  • Post category:其他
 getSpanArr(data) {
      this.spanArr = [];
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          // 如果是第一条记录(即索引是0的时候),向数组中加入1
          this.spanArr.push(1);
          this.pos = 0;
        } else {
          // console.log(data[i].id, data[i - 1].id)
          if (data[i].id === data[i - 1].id) {
            // 如果parent相等就累加,并且push 0  这里是根据一样的parent匹配
            this.spanArr[this.pos] += 1;
            this.spanArr.push(0);
          } else {
            // 不相等push 1
            this.spanArr.push(1);
            this.pos = i;
          }
        }
      }
    },





 watch: {
    data: {
      handler() {
        if(!this.data) return;
        this.getSpanArr(this.data);
      },
      deep: true,
      immediate: true
    }
  },



  //ElementUI绑定方法
 spanMethod({ row, column, rowIndex, columnIndex }) {
      if(columnIndex === 0) {
        const _row = this.spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },