首页 > 其他分享 >vue 按列合并单元格

vue 按列合并单元格

时间:2023-06-03 18:13:00浏览次数:33  
标签:vue 单元格 packNoList 按列 relaName packNo key var dscntBkAreaNm

要求

使用vue对每页表格进行单元格合并,每列纵向一致的内容合并,但要参照第一列合并情况,后面的列不能跨列合并,第二列和最后一列不合并。

思路

将首列进行合并单元格计算,然后依次将后面列按照首列合并结果进行分组,然后依次给每个分组进行合并单元格计算,最后依次将该列的多个分组内容合并。
完整的html代码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue Table with Merged Cells</title>
    <link
      rel="stylesheet"
      href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
    />
  </head>

  <body>
    <div id="app">
      <template>
        <div>
          <el-table
            :data="billList"  border style="width: 100%; margin-top: 20px"  :span-method="objectSpanMethod">
            <el-table-column prop="packNo" label="票据包号"> </el-table-column>
            <el-table-column prop="range" label="该列无需合并">
            </el-table-column>
            <el-table-column prop="TNo" label="编号(都一致)">
            </el-table-column>
            <el-table-column prop="company" label="公司名称">
            </el-table-column>
            <el-table-column prop="DateA" label="日期">
            </el-table-column>
            <el-table-column prop="dscntAmt" label="金额">
            </el-table-column>
            <el-table-column prop="dscntBkAreaNm" label="贴现机构">
            </el-table-column>
            <el-table-column prop="relaType" label="关联人类型">
            </el-table-column>
            <el-table-column prop="relaName" label="关联人名称">
            </el-table-column>
            <el-table-column prop="relaAcc" label="账号(不合并)">
            </el-table-column>
          </el-table>
        </div>
      </template>
    </div>
  </body>

  <script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <script>
    var Main = {
      data() {
        return {
          billList: [
            {
              alarmId: "6304fcd-ythy-7689-5678",
              dscntAmt: "3,068.00",
              dscntBkAreaNm: "xx机构",
              TNo: "1234567",
              DateA: "2021-12-28",
              company: "一二三有限公司",
              packNo: "10477",
              range: "-",
              relaAcc: "1",
              relaName: "衣",
              relaType: "类型A",
            },
            {
              alarmId: "6304fcd-ythy-7689-5678",
              dscntAmt: "515,123.89",
              dscntBkAreaNm: "xx机构",
              TNo: "1234567",
              DateA: "2021-12-28",
              company: "一二三有限公司",
              packNo: "68083",
              range: "-",
              relaAcc: "2",
              relaName: "小儿",
              relaType: "类型A",
            },
            {
              alarmId: "6304fcd-ythy-7689-5678",
              dscntAmt: "3,111.00",
              dscntBkAreaNm: "xx机构",
              TNo: "1234567",
              DateA: "2021-12-28",
              company: "一二三有限公司",
              packNo: "10477",
              range: "-",
              relaAcc: "3",
              relaName: "小三年",
              relaType: "类型B",
            },
            {
              alarmId: "6304fcd-ythy-7689-5678",
              dscntAmt: "3,000.00",
              dscntBkAreaNm: "xx机构",
              TNo: "1234567",
              DateA: "2021-12-28",
              company: "一二三有限公司",
              packNo: "10477",
              range: "-",
              relaAcc: "4",
              relaName: "小似",
              relaType: "类型C",
            },
            {
              alarmId: "6304fcd-ythy-7689-5678",
              dscntAmt: "3,000.00",
              dscntBkAreaNm: "xx机构",
              TNo: "1234567",
              DateA: "2021-12-28",
              company: "一二三有限公司",
              packNo: "10477",
              range: "-",
              relaAcc: "5",
              relaName: "无",
              relaType: "类型C",
            },
            {
              alarmId: "6304fcd-ythy-7689-5678",
              dscntAmt: "515,456.89",
              dscntBkAreaNm: "xx机构",
              TNo: "1234567",
              DateA: "2021-12-28",
              company: "一二三有限公司",
              packNo: "68083",
              range: "-",
              relaAcc: "6",
              relaName: "流",
              relaType: "类型B",
            },
            {
              alarmId: "6304fcd-ythy-7689-5678",
              dscntAmt: "515,456.89",
              dscntBkAreaNm: "xx机构",
              TNo: "1234567",
              DateA: "2021-12-28",
              company: "一二三有限公司",
              packNo: "68083",
              range: "-",
              relaAcc: "7",
              relaName: "其",
              relaType: "类型C",
            },
            {
              alarmId: "6304fcd-ythy-7689-5678",
              dscntAmt: "316,133.33",
              dscntBkAreaNm: "xx机构",
              TNo: "1234567",
              DateA: "2023-2-31",
              company: "一二三有限公司",
              packNo: "07536",
              range: "-",
              relaAcc: "8",
              relaName: "八",
              relaType: "类型A",
            },
            {
              alarmId: "6304fcd-ythy-7689-5678",
              dscntAmt: "2,070,658.92",
              dscntBkAreaNm: "xx机构",
              TNo: "1234567",
              DateA: "2023-2-31",
              company: "一二三有限公司",
              packNo: "07536",
              range: "-",
              relaAcc: "9",
              relaName: "旧9",
              relaType: "类型A",
            },
            {
              alarmId: "6304fcd-ythy-7689-5678",
              dscntAmt: "316,133.33",
              dscntBkAreaNm: "xx机构",
              TNo: "1234567",
              DateA: "2023-2-31",
              company: "一二三有限公司",
              packNo: "07536",
              range: "-",
              relaAcc: "10",
              relaName: "市",
              relaType: "类型B",
            },
          ],
          //spanArr用于存放每一行记录的合并数
          all: [],
          packNoList: [],
        };
      },
      mounted() {
        let packNoDot = 0;
        this.billList.forEach((item, index) => {
          if (index === 0) {
            this.packNoList.push(1);
          } else {
            if (item.packNo === this.billList[index - 1].packNo) {
              this.packNoList[packNoDot] += 1;
              this.packNoList.push(0);
            } else {
              packNoDot = index;
              this.packNoList.push(1);
            }
          }
        });

        this.all[0] = this.packNoList;
        this.all[1] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];

        this.all[2] = this.getGroupArray(
          this.getblankArray(this.packNoList, "TNo")
        );
        this.all[3] = this.getGroupArray(
          this.getblankArray(this.packNoList, "company")
        );
        this.all[4] = this.getGroupArray(
          this.getblankArray(this.packNoList, "DateA")
        );
        this.all[5] = this.getGroupArray(
          this.getblankArray(this.packNoList, "dscntAmt")
        );
        this.all[6] = this.getGroupArray(
          this.getblankArray(this.packNoList, "dscntBkAreaNm")
        );
        this.all[7] = this.getGroupArray(
          this.getblankArray(this.packNoList, "relaType")
        );
        this.all[8] = this.getGroupArray(
          this.getblankArray(this.packNoList, "relaName")
        );
        this.all[9] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];

        for (var key in this.all) {
          console.log(key + " : " + this.all[key]);
        }
      },
      methods: {
        objectSpanMethod({ row, column, rowIndex, columnIndex }) {
          const _row = this.all[columnIndex][rowIndex];
          const _col = _row > 0 ? 1 : 0;
          //该形式为行合并
          return {
            rowspan: _row,
            colspan: _col,
          };
        },
        //分组计算每组合并,并将结果
        getGroupArray(temp) {
          var tempSub = [];
          for (var key in temp) {
            if (temp[key].length === 1) {
              tempSub.push(1);
            } else {
              var tempSubSub = [];
              var tempSubSubDot = 0;
              temp[key].forEach((item, index) => {
                if (index === 0) {
                  tempSubSub.push(1);
                } else {
                  if (item === temp[key][index - 1]) {
                    tempSubSub[tempSubSubDot] += 1;
                    tempSubSub.push(0);
                  } else {
                    tempSubSubDot = index;
                    tempSubSub.push(1);
                  }
                }
              });

              for (var key in tempSubSub) {
                tempSub.push(tempSubSub[key]);
              }
            }
          }
          return tempSub;
        },
        // 获取该列按照第一列分组后的数组
        getblankArray(packNoList, columnName) {
          var temp = [];
          for (var key in this.packNoList) {
            var subTemp = [];
            for (k = 0; k < this.packNoList[key]; k++) {
              subTemp.push("");
            }
            if (subTemp.length > 0) {
              temp.push(subTemp);
            }
          }

          var s1 = 0;
          for (var key in temp) {
            for (i = 0; i < temp[key].length; i++) {
              temp[key][i] = this.billList[s1][columnName];
              s1 = s1 + 1;
            }
          }
          return temp;
        },
      },
    };
    var Ctor = Vue.extend(Main);
    new Ctor().$mount("#app");
  </script>
</html>

效果如下:

标签:vue,单元格,packNoList,按列,relaName,packNo,key,var,dscntBkAreaNm
From: https://www.cnblogs.com/sunday866/p/17454325.html

相关文章

  • 【由浅到深】聊聊 Vue 和 React 的区别,看看你在哪个段位
    大家好,我是沐华。最近部门招人,捞了一批简历至少都是5年以上的前端来面试,其中不乏360的,腾讯的,简历上写的基本都是熟练使用Vue2、Vue3、React并阅读源码对其实现原理有自己的理解,实际问起来却不免让人唏嘘比如:既然两个框架都用过,那能说一下你觉得这俩有什么区别吗毕竟是两个框架,不......
  • Vue插值语法,文本指令,事件指令v-on,属性指令v-bind
    Vue插值语法:总结:插值语法使用{{}}传入变量,相当于形参  script中data中传入变量值,相当于实参,vue将data的值传给{{}}中html中:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><scr......
  • 大件货运系统源码,技术架构:spring boot、mybatis、redis、vue、element-ui
    网络货运平台源码网络货运平台的功能网络货运是指利用互联网平台,通过物流配送的方式进行商品销售和物流运输的一种新型商业模式。这种模式将传统的货运模式与互联网技术相结合,通过网络平台进行交易、物流配送和结算等一系列流程,从而实现货物的快速、高效、便捷地运输。技术架构:spr......
  • vue3 el-image图片资源的使用
    在项目中将img文件放到/src/assets/,相关组件文件配置:<el-imagesrc="../assets/hamburger.png"/>使用以上语句提示404错误。 查询网上资料才发现原来图片资源文件一般存放在/public目录下,将文件复制到/public目录下后404错误消失,图片显示正常。Remark。......
  • JAVA的springboot+vue医疗预约服务管理信息系统,医院预约管理系统,附源码+数据库+论文+P
    1、项目介绍会员制医疗预约服务管理信息系统是针对会员制医疗预约服务管理方面必不可少的一个部分。在会员制医疗预约服务管理的整个过程中,会员制医疗预约服务管理系统担负着最重要的角色。为满足如今日益复杂的管理需求,各类的管理系统也在不断改进。本课题所设计的是会员制医疗......
  • 从0搭建Vue3组件库(十三):引入Husky规范git提交
    为什么要引入husky?虽然我们项目中引入了prettier和eslint对代码格式进行了校验,但是多人开发的时候难免依然会有人提交不符合规范的代码到仓库中,如果我们拉取到这种代码还得慢慢对其进行修改,这是一件很麻烦的事情,同时也为了避免团队成员提交五花八门message,因此我们可以引......
  • Intersection Observer API 交叉观察器 API vue3 antd table 滚动加载 使用过程
    需求:表格滚动加载做法:步骤一:给表格最后一行添加特定标识,类名或者id等组件库https://www.antdv.com/components/table-cn#APIwebApihttps://developer.mozilla.org/zh-CN/docs/Web/API/Intersection_Observer_API组件名table添加类名的组件方法rowClassName使用例子::......
  • Vue路由传参的几种方式
    vue路由传参是指嵌套路由时父路由向子路由传递参数,否则操作无效一、利用"router-link"路由导航父组件:使用 <router-linkto="/跳转路径/传入的参数"></router-link>例如:<router-linkto="/a/123">routerlink传参</router-link>子组件:使用this.$route.params.num接收父......
  • vue+elementUI 合并行3
    1、返回数据为一维数组getData(){that.tableData=res.data.data.list;}2、合并函数挂在vue底层:Vue.prototype.$spanMethodFunc=function(list,props,row,col)写在method内:spanMethodFunc(list,props,row,col){if(col>=props.length||!props[co......
  • Vue进阶(幺零八):npm run build 错误 (node:7852) UnhandledPromiseRejectionWarning: Cs
    (文章目录)一、前言在项目打包过程中,突然报如下错误:Vuenpmrunbuild错误(node:7852)UnhandledPromiseRejectionWarning:CssSyntaxError:xxxx.但是在执行npmrundev过程中,并未错误或告警信息。二、解决方案打开webpack.prod.conf.js,注释掉以下配置代码newOptimiz......