根據某一列屬性合並element表格行


轉自:https://www.cnblogs.com/chen-yi-yi/p/11224346.html

通過 span-method 綁定方法 objectSpanMethod方法
   this.getSpanArr(this.tableData); //this.tableData 指接口取到的數據
 // 2、 因為要合並的行數是不固定的,此函數是實現合並隨意行數的功能
    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 {
          if (data[i].parentCode === data[i - 1].parentCode) {
            // 如果parentCode相等就累加,並且push 0  這里是根據一樣的parentCode匹配 
            this.spanArr[this.pos] += 1;
            this.spanArr.push(0);
          } else {
            // 不相等push 1
            this.spanArr.push(1);
            this.pos = i;
          }
        }
      }
    },
    // 3、合並行數
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // columnIndex === 0 找到第一列,實現合並隨機出現的行數
      if (columnIndex === 0) {
        const _row = this.spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        };
      }
    },

  可根據需求進行調整,上面的只是第一列合並,如果需求需要多列內容分別合並,且后面幾列需要在前一列合並的基礎上合並,該怎么做呢?

/**
     * 獲取XXXXX列數據,判斷上下兩個數據是否一致
     * 將對應列數據初始化為空數組,第一個數據默認為1
     * 如果上下數據一致,則對應下標的數值+1,並同時push一個0
     * 示例:【1,1,2,0,1】則表示第三行數據合並,合並兩行,並刪除第四行數據
     */
    getMatterArr(data, name) {
      this.matterArr = [];
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.matterArr.push(1);
          this.pos = 0;
        } else {
          if (data[i][name] === data[i - 1][name]) {
            this.matterArr[this.pos] += 1;
            this.matterArr.push(0);
          } else {
            this.matterArr.push(1);
            this.pos = i;
          }
        }
      }
    },
    /**
     * 獲取XXXX列數據,判斷上下兩個數據是否一致
     * 將對應列數據初始化為空數組,第一個數據默認為1
     * 如果上下數據一致,則對應下標的數值+1,並同時push一個0
     * 示例:【1,1,2,0,1】則表示第三行數據合並,合並兩行,並刪除第四行數據
     */
    getReMatterArr(data, name) {
      this.reMatterArr = [];
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.reMatterArr.push(1);
          this.index = 0;
        } else {
          if (data[i][name] === data[i - 1][name]) {
            this.reMatterArr[this.index] += 1;
            this.reMatterArr.push(0);
          } else {
            this.reMatterArr.push(1);
            this.index = i;
          }
        }
      }
    },
    /**
     * 合並行數據
     * 如果抵消事項數據第二個數據和前一個數據一樣,則將其行進行合並
     * 報表事項則先判斷對應的抵消事項是否合並,如果抵消事項處於合並狀態,則其后面的報表事項允許合並
     * @param rowIndex第幾行,columnIndex第幾列
     * @param matterCode,第一個判斷數據是否一致的字段
     * @param secondColumnKey 第二列判斷是否一致的字段(該字段是由第一列標識 + 第二列標識合並得到的,由后台返回的 )
     */
    mergeColumn({ row, column, rowIndex, columnIndex }) {
      // columnIndex === 0 找到第一列,實現抵消事項合並
      if (columnIndex === 0) {
        this.getMatterArr(this.pageData, "matterCode");
        const _row = this.matterArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col,
        };
      }
      if (columnIndex === 1) {
        //第二列報表事項的合並
        this.getReMatterArr(this.pageData, "secondColumnKey");
        const _row = this.reMatterArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col,
        };
      }
    },

  getReMatterArr與getMatterArr 方法實際上是一個功能,但因為執行順序的原因所以分開了,暫時沒想到好的能合並到一起的方法。

  secondColumnKey參數實際上是第一列數據標識+第二列標識組合形成的標識,這樣判斷就會在第一列相同的(也就是合並的狀態)時候才會進行第二列的合並。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM