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',
      ],
  1. 由于我的表格只需要固定编码和名字,针对这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];
    },

相关推荐
雷神乐乐3 分钟前
创建前端项目的方法
前端·javascript·vue.js
prince_zxill10 分钟前
JavaScript面向对象编程:Prototype与Class的对比详解
前端·javascript·ecmascript·原型模式
计算机-秋大田42 分钟前
基于SpringBoot的美食烹饪互动平台的设计与实现(源码+SQL脚本+LW+部署讲解等)
vue.js·spring boot·后端·课程设计·美食
D.eL1 小时前
Vue 2 项目中 Mock.js 的完整集成与使用教程
前端·javascript·vue.js
brzhang1 小时前
墙裂推荐一个在 Apple Silicon 上创建和管理虚拟机的轻量级开源工具:lume
前端·后端
轻口味2 小时前
Vue.js 响应式引用与响应式数据(`ref` 和 `reactive`)
vue.js
Along丶WG2 小时前
解决国内服务器 npm install 卡住的问题
前端·npm·node.js
prince_zxill2 小时前
Node.js 和 npm 安装教程
前端·javascript·vue.js·npm·node.js
弄不死的强仔3 小时前
可被electron等调用的Qt截图-录屏工具【源码开放】
前端·javascript·qt·electron·贴图·qt5
霸王蟹3 小时前
el-table组件样式如何二次修改?
前端·javascript·vue.js·笔记·学习·前端框架