Vue 学习随笔系列十三 -- ElementUI 表格归并单元格

[复制链接]
发表于 2026-1-1 13:34:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
ElementUI 表格归并单元格



一、表头归并

参考:
二、单元格归并

1、示例代码

  1. <template>
  2.   <div>
  3.     <el-table
  4.       size="small"
  5.       border
  6.       class="custom-tab"
  7.       :data="tableBody"
  8.       :span-method="objectMergeMethod"
  9.       :cell-style="columnStyle"
  10.       :row-class-name="rowStyle"
  11.       :header-cell-style="{
  12.         background: '#87CEFA',
  13.         color: '#38434F',
  14.         fontWeight: '500',
  15.         fontSize: '14px',
  16.       }"
  17.     >
  18.    
  19.       <!-- <el-table-column type="index" label="序号" width="58" align="center"></el-table-column> -->
  20.       <el-table-column prop="type" label="姓名" align="center" width="140px"></el-table-column>
  21.       <el-table-column prop="name" label="科目" align="center"></el-table-column>
  22.       <el-table-column prop="sysData" label="成绩" align="center"></el-table-column>
  23.       <el-table-column prop="contractData" label="排名" align="center"></el-table-column>
  24.     </el-table>
  25.   </div>
  26. </template>
  27. <script>
  28. export default {
  29.   props: {
  30.     DiffData: {},
  31.     isShowOpr: {
  32.       default: false
  33.     },
  34.   },
  35.   data() {
  36.     return {
  37.       // 表体数据
  38.       tableBody: [
  39.         {
  40.           type: "小明",
  41.           name: "语文",
  42.           sysData: 10,
  43.           contractData: "1",
  44.           isDiff: "是",
  45.           fhsj: "",
  46.         },
  47.         {
  48.           type: "小明",
  49.           name: "数学",
  50.           sysData: 20,
  51.           contractData: "2",
  52.           isDiff: "是"
  53.         },
  54.         {
  55.           type: "小明",
  56.           name: "英语",
  57.           sysData: 30,
  58.           contractData: "1",
  59.           isDiff: "是"
  60.         },
  61.         {
  62.           type: "小明",
  63.           name: "地理",
  64.           sysData: 30,
  65.           contractData: "1",
  66.           isDiff: "是"
  67.         },
  68.         {
  69.           type: "小明",
  70.           name: "生物",
  71.           sysData: 30,
  72.           contractData: "1",
  73.           isDiff: "是"
  74.         },
  75.         {
  76.           type: "小李",
  77.           name: "语文",
  78.           sysData: 88,
  79.           contractData: "1",
  80.           isDiff: "是"
  81.         },
  82.         {
  83.           type: "小李",
  84.           name: "数学",
  85.           sysData: 44,
  86.           contractData: "1",
  87.           isDiff: "是"
  88.         },
  89.         {
  90.           type: "小李",
  91.           name: "英语",
  92.           sysData: 44,
  93.           contractData: "1",
  94.           isDiff: "是"
  95.         },
  96.         {
  97.           type: "小李",
  98.           name: "历史",
  99.           sysData: 44,
  100.           contractData: "1",
  101.           isDiff: "是"
  102.         },
  103.         {
  104.           type: "小李",
  105.           name: "生物",
  106.           sysData: 44,
  107.           contractData: "1",
  108.           isDiff: "是"
  109.         },
  110.         {
  111.           type: "小王",
  112.           name: "语文",
  113.           sysData: 1001,
  114.           contractData: "1",
  115.           isDiff: "是"
  116.         },
  117.         {
  118.           type: "小王",
  119.           name: "数学",
  120.           sysData: 1001,
  121.           contractData: "1",
  122.           isDiff: "是"
  123.         },
  124.         {
  125.           type: "小王",
  126.           name: "英语",
  127.           sysData: 1001,
  128.           contractData: "1",
  129.           isDiff: "是"
  130.         },
  131.         {
  132.           type: "小王",
  133.           name: "地理",
  134.           sysData: 1001,
  135.           contractData: "1",
  136.           isDiff: "是"
  137.         },
  138.         {
  139.           type: "小王",
  140.           name: "生物",
  141.           sysData: 1001,
  142.           contractData: "1",
  143.           isDiff: "是"
  144.         },
  145.       ],
  146.       cellList: [], // 单元格数组
  147.       count: null, // 计数
  148.     };
  149.   },
  150.   watch: {
  151.     tableBody: {
  152.       immediate: true,
  153.       deep: true,
  154.       handler() {
  155.         this.computedColumns(this.tableBody)
  156.       },
  157.     },
  158.   },
  159.   // mounted() {
  160.   //   // 第1步,根据表体信息,计算合并单元格的信息
  161.   //   this.computedColumns(this.DiffData);
  162.   // },
  163.   methods: {
  164.     columnStyle({row, column, rowIndex, columnIndex}) {
  165.       if(columnIndex === 0) {
  166.         // 表头样式
  167.         // return "background: #F5F6FA; color: #171A23; font-weight: 500; font-size: 14px; ";
  168.         if(row.type == '小明') {
  169.           return 'row-m1'
  170.         } else if(row.type == '小李') {
  171.           return 'row-m2'
  172.         } else if(row.type == '小王') {
  173.           return 'row-m3'
  174.         } else if(row.type == 'xxx') {
  175.           return 'row-m4'
  176.         }
  177.       }
  178.     },
  179.     rowStyle({ row, rowIndex }) {
  180.       if(row.type == '小明') {
  181.         return 'row-m1'
  182.       } else if(row.type == '小李') {
  183.         return 'row-m2'
  184.       } else if(row.type == '小王') {
  185.         return 'row-m3'
  186.       } else if(row.type == 'xxx') {
  187.         return 'row-m4'
  188.       }
  189.       // 指定行或列信息样式
  190.       // if(rowIndex === 7) {
  191.       //   return 'target-row'
  192.       // }
  193.     },
  194.     // 第1步,遍历表格数据
  195.     computedColumns(tableBody) {
  196.       // 循环遍历表体数据
  197.       for (let i = 0; i < tableBody.length; i++) {
  198.         if (i == 0) {
  199.           // 先设置第一项,往 cellList 中追加 1, 若下一项与当前项相同,则往 cellList中追加 0
  200.           // count 初始值为 0
  201.           this.cellList.push(1);
  202.           this.count = 0;
  203.           // console.log("索引", 0, this.count);
  204.         } else {
  205.           // 判断当前项与上一项的类别(type)是否相同,若相同则合并这一列的单元格
  206.           if (tableBody[i].type == tableBody[i - 1].type) {
  207.             // 如果相同, this.cellList 增加计数 1 ,并向数组追加数据 0
  208.             this.cellList[this.count] += 1;
  209.             this.cellList.push(0);
  210.             // console.log("索引", this.count);
  211.           } else {
  212.             // 如果不同,往cellList数组中追加 1,并将索引赋值给 count
  213.             this.cellList.push(1);
  214.             this.count = i;
  215.             // console.log("索引", this.count);
  216.           }
  217.         }
  218.       }
  219.     },
  220.     // 第2步,将计算好的结果返回给el-table,表格会根据结果做出对应合并列渲染
  221.     objectMergeMethod({ row, column, rowIndex, columnIndex }) {
  222.       // 给第一列做单元格合并。0 是第一列,1 是第二列。
  223.       if (columnIndex === 0) {
  224.         const rowCell = this.cellList[rowIndex];
  225.         if (rowCell > 0) {
  226.           const colCell = 1;
  227.           return {
  228.             rowspan: rowCell,
  229.             colspan: colCell,
  230.           };
  231.         } else {
  232.           // 清除所有单元格数据,防止动态数据出现表格偏移现象
  233.           return {
  234.             rowspan: 0,
  235.             colspan: 0,
  236.           };
  237.         }
  238.       }
  239.     },
  240.   },
  241. };
  242. </script>
  243. <style lang="scss" scoped>
  244.   .vueWrap {
  245.     .custom-tab {
  246.       margin: 0 0 20px 0;
  247.     }
  248.   }
  249.   .data-box {
  250.     display: flex;
  251.     align-items: center;
  252.     justify-content: center;
  253.     .icon {
  254.       height: 7px;
  255.       width: 7px;
  256.       background-color: '#333';
  257.       margin: 0 2px 0 0;
  258.     }
  259.   }
  260.   ::v-deep .el-table--enable-row-transition .el-table__body td.el-table__cell {
  261.   }
  262.   ::v-deep .target-row {
  263.     color: #3363FF;
  264.     font-weight: 500;
  265.   }
  266.   ::v-deep .row-m1 {
  267.     background-color: #BBFFFF;
  268.     color: #333;
  269.   }
  270.   ::v-deep .row-m2 {
  271.     background-color: #FFF8DC;
  272.     color: #333;
  273.   }
  274.   ::v-deep .row-m3 {
  275.     background-color: #F0FFF0;
  276.     color: #333;
  277.   }
  278.   ::v-deep .row-m4 {
  279.     background-color: #E0EEE0;
  280.     color: #333;
  281.   }
  282.   .update-btn {
  283.     color: #4597EB;
  284.     cursor: pointer;
  285.   }
  286. </style>
复制代码
2、示例效果


回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表