Vue - ElementUI 表格合并行或列

ElementUI官网:https://element.eleme.cn/#/zh-CN/component/table#he-bing-xing-huo-lie

效果预览

在这里插入图片描述

代码实现

<template>
  <div class="page">
    <el-table
      class="tabel"
      :data="tableData"
      :span-method="arraySpanMethod"
      :header-cell-style="{ background: '#029ef1', color: '#ffffff' }"
      border
    >
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="amount1" label="数值 1"> </el-table-column>
      <el-table-column prop="amount2" label="数值 2"> </el-table-column>
      <el-table-column prop="amount3" label="数值 3"> </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          id: "12987122",
          name: "王小虎1",
          amount1: "234",
          amount2: "3.2",
          amount3: 10,
        },
        {
          id: "12987123",
          name: "王小虎2",
          amount1: "165",
          amount2: "4.43",
          amount3: 12,
        },
        {
          id: "12987124",
          name: "王小虎3",
          amount1: "324",
          amount2: "1.9",
          amount3: 9,
        },
        {
          id: "12987125",
          name: "王小虎4",
          amount1: "621",
          amount2: "2.2",
          amount3: 17,
        },
        {
          id: "12987126",
          name: "王小虎5",
          amount1: "539",
          amount2: "4.1",
          amount3: 15,
        },
        {
          id: "12987127",
          name: "王小虎5",
          amount1: "555",
          amount2: "4.2",
          amount3: 15,
        },
        {
          id: "12987128",
          name: "王小虎6",
          amount1: "555",
          amount2: "4.3",
          amount3: 16,
        },
      ],
    };
  },
  methods: {
    // 获取合并的数组
    flitterData(arr) {
      // 第二列(name)合并列的数据
      let spanOneArr = [];
      let concatOne = 0;
      // 第三列(amount1)合并列的数据
      let spanTwoArr = [];
      let concatTwo = 0;
      // 第五列(amount3)合并列的数据
      let spanThree = [];
      let concatThree = 0;
      //    ......
      arr.forEach((item, index) => {
        console.log(item, index);
        if (index === 0) {
          spanOneArr.push(1);
          spanTwoArr.push(1);
          spanThree.push(1);
        } else {
          // 第二列(name)需合并相同内容的判断条件
          if (item.name === arr[index - 1].name) {
            spanOneArr[concatOne] += 1;
            spanOneArr.push(0);
          } else {
            spanOneArr.push(1);
            concatOne = index;
          }
          // 第三列(amount1)需合并相同内容的判断条件
          if (item.amount1 === arr[index - 1].amount1) {
            spanTwoArr[concatTwo] += 1;
            spanTwoArr.push(0);
          } else {
            spanTwoArr.push(1);
            concatTwo = index;
          }
          // 第五列(amount3)需合并相同内容的判断条件
          if (item.amount3 === arr[index - 1].amount3) {
            spanThree[concatThree] += 1;
            spanThree.push(0);
          } else {
            spanThree.push(1);
            concatThree = index;
          }
        }
      });
      return {
        one: spanOneArr,
        two: spanTwoArr,
        three: spanThree,
      };
    },
    /**
     * 合并表格列
     * row 当前行
     * column 当前列
     * rowIndex 当前行号
     * columnIndex 当前列号
     */
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      /**合并列 */
      // id 为 12987122、12987124 的1、2列合并,4、5列合并
      if (row.id == "12987122" || row.id == "12987124") {
        if (columnIndex === 0) {
          return [1, 2];
        } else if (columnIndex === 1) {
          return [0, 0];
        } else if (columnIndex === 3) {
          return [1, 2];
        } else if (columnIndex === 4) {
          return [0, 0];
        }
      }
      // id 为 12987123 的2、3列合并
      if (row.id == "12987123") {
        if (columnIndex === 1) {
          return [1, 2];
        } else if (columnIndex === 2) {
          return [0, 0];
        }
      }
      /**合并行 */
      // 第二列(name)的属性值相同的行合并
      if (columnIndex === 1) {
        const _row = this.flitterData(this.tableData).one[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col,
        };
      }
      // 第三列(amount1)的属性值相同的行合并
      if (columnIndex === 2) {
        const _row = this.flitterData(this.tableData).two[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col,
        };
      }
      // 第五列(amount3)的属性值相同的行合并
      if (columnIndex === 4) {
        const _row = this.flitterData(this.tableData).three[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col,
        };
      }
    },
  },
};
</script>

<style scoped>
.page {
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
  background: #e9e9e9;
}
.tabel {
  width: 60%;
}
.tabel >>> td {
  background-color: #92c2f1;
  color: #040404;
}
</style>
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现点击合并表格数据,需要使用 Element UI 提供的表格组件和相关 API。以下是一个基本的实现方法: 1. 定义表格的数据格式,包括表头和内容。 2. 在表格组件中使用 `table-row-class-name` 属性来设置行的样式名。这个属性接受一个函数,函数的参数为当前行的数据对象,返回值为这一行的样式名。 3. 在样式名函数中,根据当前行的数据对象判断是否需要和前一行合并,如果需要合并,则返回一个特定样式名。 4. 使用 `span-method` 属性来定义合并单元格的方法。这个属性接受一个函数,函数的参数为当前单元格的行列信息,返回值为一个包含 `rowspan` 和 `colspan` 属性的对象,用于指定合并的行数和列数。 下面是一个示例代码: ```html <template> <el-table :data="tableData" :row-class-name="rowClassName" :span-method="spanMethod"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <<el-table-column prop="gender" label="性别"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 20, gender: '男' }, { name: '李四', age: 22, gender: '男' }, { name: '王五', age: 22, gender: '女' }, { name: '赵六', age: 25, gender: '女' }, ], }; }, methods: { rowClassName(row, index) { if (index > 0 && row.gender === this.tableData[index - 1].gender) { // 如果和前一行性别相同,则需要合并 return 'merge-row'; } return ''; }, spanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 2 && rowIndex > 0 && row.gender === this.tableData[rowIndex - 1].gender) { // 如果是第三列,并且和前一行性别相同,则需要合并 return { rowspan: 0, colspan: 0 }; } }, }, }; </script> <style> .merge-row td:nth-child(3) { /* 第三列需要合并 */ border-top-width: 0 !important; /* 去掉上边框 */ } </style> ``` 在上面的代码中,我们定义了一个表格,包含三列:姓名、年龄、性别。然后,我们使用 `row-class-name` 属性来设置行的样式名,如果一行需要和前一行合并,则返回一个特定样式名。在样式中,我们设置第三列的单元格不显示上边框。最后,我们使用 `span-method` 属性来定义合并单元格的方法,如果一个单元格需要合并,则返回一个包含 `rowspan` 和 `colspan` 属性的对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值