自动合并单元格

80 阅读1分钟

 <vxe-table
      border
      highlight-hover-row
      highlight-current-row
      show-all-overflow
      height="550"
      class="lossTaget"
      :row-class-name="rowClassName"
      :header-cell-class-name="headerCellClassName"
      :cell-class-name="cellClassName"
      :data="hsstandardData"
      align="center"
      :span-method="colspanMethod"
      style="width:100%"
    >
    </vxe-table>
    mergeCells (arr) {
        let colspanResult = [] // 合并的结果:[2, 0, 1, 3, 0, 0],有相同的第一个累加,后面的都为0
        let colspanIndex = 0
        arr.forEach((item, index) => {
          // 如果是第一项:默认给1
          if (index === 0) {
            colspanResult.push(1)
          } else {
            // 如果不是第一项:
            // 该项与前一项相比字段:
            if (item.processType === arr[index - 1].processType) {
              // 第2列需合并相同内容的判断条件
              colspanResult[colspanIndex] += 1
              colspanResult.push(0)
            } else {
              colspanResult.push(1)
              colspanIndex = index
            }
          }
        })
        return colspanResult
      },
      colspanMethod ({ rowIndex, column, data }) {
        if (column && column['title'] === '制程') {
          let arr = this.mergeCells(data)
          const _row = arr[rowIndex]
          const _col = _row > 0 ? 1 : 0
          let a = {
            rowspan: _row,
            colspan: _col
          }
          return a
        }
      },