vue项目- el-table表格合并行

1.再页面中表格加上行方法

  <el-table

            :span-method="objectSpanMethod"

</el-table>

2,定义2个变量

  spanArr: [], //存储合并单元格的开始位置
      colFields: [
        'itemCode',
        'itemDesc',
        'purchaseApplyNo',
        'purchaseApplyLinenum',
        'purchaseOrderNo',
        'purchaseOrderLinenum',
        'purchaseMode',
        'purchaseMethod',
        'materielSection',
        'materielNo',
        'materielDesc',
        'num',
        'unit',
        'supplierName',
        'technologyId',
        'biddingPlanNo',
        'biddingBatch',
        'outbidDate',
        'frameworkAgreementNo',
        'eCommerceNo',
        'applyUser',
        'applyDate',
        'purchaseTime',
        'plannedDeliveryTime',
        'outbid',
        'contractNo',
        'orderTotalNetPrice',
        'orderTaxInclusivePrice',
        'orderDate',
        'handOverNum',
        'handOverDate',
        'checkNum',
        'checkDate',
        'wbsNo',
        'wbsDesc',
      ],

3. 由于我的表格只需要固定编码和名字,针对这2个字段的判断处理

 getSpanArr() {
      for (let i = 0; i < this.tableData.length; i++) {
        let row = i;
        // let col = i % this.colCount;
        if (row === 0) {
          // i 表示行 j表示列
          for (let j = 0; j < this.colFields.length; j++) {
            this.spanArr[i * this.colFields.length + j] = {
              rowspan: 1,
              colspan: 1,
            };
          }
        } else {
          for (let j = 0; j < this.colFields.length; j++) {
            // 当前和上一次的一样
            //  合并所有列的相同数据单元格
            if (
              this.colFields[j] == 'itemCode' ||
              this.colFields[j] == 'itemDesc'
            ) {
              // 指定合并哪些列
              if (
                this.tableData[row]['itemCode'] !==
                  this.tableData[row - 1]['itemCode'] ||
                this.tableData[row]['itemDesc'] !==
                  this.tableData[row - 1]['itemDesc']
              ) {
                // 哪些不合并:itemCode不一样的,不合并
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1,
                  colspan: 1,
                };
              } else if (
                this.tableData[row][this.colFields[j]] ===
                this.tableData[row - 1][this.colFields[j]]
              ) {
                let beforeItem =
                  this.spanArr[(row - 1) * this.colFields.length + j];
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1 + beforeItem.rowspan, // 合并几列
                  colspan: 1, // 合并几行
                };
                beforeItem.rowspan = 0;
                beforeItem.colspan = 0;
              } else {
                // rowspan 和 colspan 都为1表格此单元格不合并
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1,
                  colspan: 1,
                };
              }
            }
          }
        }
      }
      // 对数据进行倒序
      let stack = [];
      for (let i = 0; i < this.colFields.length; i++) {
        for (let j = 0; j < this.tableData.length; j++) {
          // console.log("i=" + i + " j=" + j);
          // i 表示列 j表示行
          if (j === 0) {
            if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
              stack.push(this.spanArr[j * this.colFields.length + i]);
            }
          } else {
            if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
              stack.push(this.spanArr[j * this.colFields.length + i]);
            } else {
              stack.push(this.spanArr[j * this.colFields.length + i]);
              while (stack.length > 0) {
                let pop = stack.pop();
                let len = stack.length;
                this.spanArr[(j - len) * this.colFields.length + i] = pop;
              }
            }
          }
        }
      }
      // console.log(this.spanArr);
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // console.log(this.spanArr[rowIndex * this.colFields.length + columnIndex]);
      return this.spanArr[rowIndex * this.colFields.length + columnIndex];
    },

相关推荐

  1. vue项目- el-table表格合并

    2024-03-19 15:28:02       18 阅读
  2. vue3中el-table实现表格合计

    2024-03-19 15:28:02       40 阅读
  3. el-table 合集合并

    2024-03-19 15:28:02       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-19 15:28:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-19 15:28:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-19 15:28:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-19 15:28:02       18 阅读

热门阅读

  1. Prometheus云原生监控笔记

    2024-03-19 15:28:02       17 阅读
  2. c++希尔排序

    2024-03-19 15:28:02       17 阅读
  3. AI论文:中国A股市场长期牛市的先决条件研究

    2024-03-19 15:28:02       18 阅读
  4. ZYNQ NE10 裸机(standalone)

    2024-03-19 15:28:02       20 阅读
  5. Pytorch nn.Module

    2024-03-19 15:28:02       19 阅读
  6. amazon linux 2023安装redis6

    2024-03-19 15:28:02       21 阅读
  7. C#异步运行任务

    2024-03-19 15:28:02       17 阅读
  8. 基于自然语言处理的情感分析系统

    2024-03-19 15:28:02       18 阅读