vue element ui表格相同数据合并单元格效果实例

 更新时间:2023年11月22日 10:04:47   作者:弈425  
工作中遇到需要根据单元格某个属性合并,特此记录下,下面这篇文章主要给大家介绍了关于vue element ui表格相同数据合并单元格效果的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下

先看效果  

前提是你的数据是扁平的数据因为要根据上下数据是否一样才合并的  如果是子级数据需要改一下数据格式了

下面是数据的样式

tableData: [{
          id: '1',
          name: '王小虎',
          amount1: '165',
          amount2: '3.2',
          amount3: 10
        }, {
          id: '1',
          name: '王小虎',
          amount1: '162',
          amount2: '4.43',
          amount3: 12
        }, {
          id: '1',
          name: '王we虎',
          amount1: '621',
          amount2: '1.9',
          amount3: 9
        }, {
          id: '2',
          name: '王we虎',
          amount1: '621',
          amount2: '2.2',
          amount3: 17
        }, {
          id: '3',
          name: '王小虎',
          amount1: '621',
          amount2: '4.1',
          amount3: 15
        }],

合并单元格的重点属性就是 :summary-method="" 这个是关键

<el-table ref="tableRef" show-summary :summary-method="getSummaries" :span-method="handleSpanMethod"  :data="listData"  border style="width: 100%; margin-top: 5px">
      <el-table-column label="No." type="index" align="center" width="50"></el-table-column>
      <el-table-column prop="checkResult" label="Check result"> </el-table-column>
      <el-table-column prop="standardChineseName" label="Standard chinese name"> </el-table-column>
      <el-table-column prop="ingredientInciCtfaName" label="Ingredient INCI(CTFA) name"> </el-table-column>
      <el-table-column prop="RMInFormula" label="RM in formula(%)"> </el-table-column>
      <el-table-column prop="ingredientInRM" label="Ingredient in RM(%)"> </el-table-column>
      <el-table-column prop="ingredientInFormula" label="Ingredient in formula(%)"> </el-table-column>
      <el-table-column prop="purposeOfUse" label="Purpose of use"> </el-table-column>
      <el-table-column prop="templateUrl5" label="New RM" align="center" width="100"> </el-table-column>
    </el-table>
data() {
    return {
      listData: [],

      // 合并单元格
      column1Arr: [], // column1
      column1Index: 0, // column1索引

      column2Arr: [], // column2
      column2Index: 0, // column2索引

      column3Arr: [], // column3
      column3Index: 0, // column3索引

      column4Arr: [], // column4
      column4Index: 0, // column4索引

      column5Arr: [], // column5
      column5Index: 0, // column5索引

      column6Arr: [], // column6
      column6Index: 0, // column6索引

      column7Arr: [], // column7
      column7Index: 0, // column7索引
    }
  },
mergeTable(data) {
      if (data.length > 0) {
        for (var i = 0; i < data.length; i++) {
          if (i === 0) {
            // 第一行必须存在,以第一行为基准  合并的数量根据直接需求改
            this.column1Arr.push(1) // column1
            this.column1Index = 0
            this.column2Arr.push(1) // column2
            this.column2Index = 0

            this.column3Arr.push(1) // column3
            this.column3Index = 0

            this.column4Arr.push(1) // column4
            this.column4Index = 0

            this.column5Arr.push(1) // column5
            this.column5Index = 0

            this.column6Arr.push(1) // column6
            this.column6Index = 0

            this.column7Arr.push(1) // column7
            this.column7Index = 0
          } else {
            // 判断当前元素与上一元素是否相同   
            // 我是根据第一个数据合并后续才可以合并 
            //如果是 根据当前表格的前一列可以在每一个 column1++   后面添加上前一个表格的列            
            // 如果是只要相同就合并那就每个column 里面的条件直接当前列就可以了
    
            // column1
            if (data[i].checkResult === data[i - 1].checkResult) {
              this.column1Arr[this.column1Index] += 1
              this.column1Arr.push(0)
            } else {
              this.column1Arr.push(1)
              this.column1Index = i
            }

            // column2
              if (data[i].standardChineseName === data[i - 1].standardChineseName && data[i].checkResult === data[i - 1].checkResult) {
                this.column2Arr[this.column2Index] += 1
                this.column2Arr.push(0)
              } else {
                this.column2Arr.push(1)
                this.column2Index = i
              }

              // column3
              if (data[i].ingredientInciCtfaName === data[i - 1].ingredientInciCtfaName && data[i].checkResult === data[i - 1].checkResult) {
                this.column3Arr[this.column3Index] += 1
                this.column3Arr.push(0)
              } else {
                this.column3Arr.push(1)
                this.column3Index = i
              }

              // column4
              if (data[i].RMInFormula === data[i - 1].RMInFormula && data[i].checkResult === data[i - 1].checkResult) {
                this.column4Arr[this.column4Index] += 1
                this.column4Arr.push(0)
              } else {
                this.column4Arr.push(1)
                this.column4Index = i
              }

              // column5
              if (data[i].ingredientInRM === data[i - 1].ingredientInRM && data[i].checkResult === data[i - 1].checkResult) {
                this.column5Arr[this.column5Index] += 1
                this.column5Arr.push(0)
              } else {
                this.column5Arr.push(1)
                this.column5Index = i
              }

              // column6
              if (data[i].ingredientInFormula === data[i - 1].ingredientInFormula && data[i].checkResult === data[i - 1].checkResult) {
                this.column6Arr[this.column6Index] += 1
                this.column6Arr.push(0)
              } else {
                this.column6Arr.push(1)
                this.column6Index = i
              }

              // column7
              if (data[i].purposeOfUse === data[i - 1].purposeOfUse && data[i].checkResult === data[i - 1].checkResult) {
                this.column7Arr[this.column7Index] += 1
                this.column7Arr.push(0)
              } else {
                this.column7Arr.push(1)
                this.column7Index = i
              }
          }
        }
      }
    },
    
    //我这里是在表格的第二列开始合并因为第一列是 序号 不可以合并  从第一个开始合并就把 下表
    //改为0  columnIndex === 0
    handleSpanMethod({ rowIndex, columnIndex }) {
      if (columnIndex === 1) {
        // 第二列 column2
        const _row_1 = this.column1Arr[rowIndex]
        const _col_1 = _row_1 > 0 ? 1 : 0
        return {
          rowspan: _row_1,
          colspan: _col_1,
        }
      } 
      else if (columnIndex === 2) {
        // 第三列 column3
        const _row_2 = this.column2Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2,
        }
      } else if (columnIndex === 3) {
        // 第四列 column4
        const _row_3 = this.column3Arr[rowIndex]
        const _col_3 = _row_3 > 0 ? 1 : 0
        return {
          rowspan: _row_3,
          colspan: _col_3,
        }
      } else if (columnIndex === 4) {
        // 第五列 column5
        const _row_4 = this.column4Arr[rowIndex]
        const _col_4 = _row_4 > 0 ? 1 : 0
        return {
          rowspan: _row_4,
          colspan: _col_4,
        }
      } else if (columnIndex === 5) {
        // 第六列 column6
        const _row_5 = this.column5Arr[rowIndex]
        const _col_5 = _row_5 > 0 ? 1 : 0
        return {
          rowspan: _row_5,
          colspan: _col_5,
        }
      } else if (columnIndex === 6) {
        // 第七列 column7
        const _row_6 = this.column6Arr[rowIndex]
        const _col_6 = _row_6 > 0 ? 1 : 0
        return {
          rowspan: _row_6,
          colspan: _col_6,
        }
      } else if (columnIndex === 7) {
        // 第八列 column8
        const _row_7 = this.column7Arr[rowIndex]
        const _col_7 = _row_7 > 0 ? 1 : 0
        return {
          rowspan: _row_7,
          colspan: _col_7,
        }
      }
    },
//调取接口获取数据
getList() {
      const prams = {
        taskId: this.taskId,
        formulaId: this.formulaId
      }
      this.$http({
        headers: {
          'Content-Type': 'application/json',
        },
        method: 'post',
        url: '/123123',
        data: prams,
      })
      .then(res=>{
        this.listData = res.data.records //给data变量赋值
        this.mergeTable(this.listData)//合并单元格传入数据
      })
      //清空之前的数据  不清空 表格数据就会乱套
      this.column1Arr = []
      this.column1Index = 0
      this.column2Arr = []
      this.column2Index = 0
      this.column3Arr = []
      this.column3Index = 0
      this.column4Arr = []
      this.column4Index = 0
      this.column5Arr = []
      this.column5Index = 0
      this.column6Arr = []
      this.column6Index = 0
      this.column7Arr = []
      this.column7Index = 0
    },

完整代码

<template>
  <div>
    <el-table ref="tableRef" show-summary :summary-method="getSummaries" :span-method="handleSpanMethod"  :data="listData"  border style="width: 100%; margin-top: 5px">
      <el-table-column label="No." type="index" align="center" width="50"></el-table-column>
      <el-table-column prop="checkResult" label="Check result"> </el-table-column>
      <el-table-column prop="standardChineseName" label="Standard chinese name"> </el-table-column>
      <el-table-column prop="ingredientInciCtfaName" label="Ingredient INCI(CTFA) name"> </el-table-column>
      <el-table-column prop="RMInFormula" label="RM in formula(%)"> </el-table-column>
      <el-table-column prop="ingredientInRM" label="Ingredient in RM(%)"> </el-table-column>
      <el-table-column prop="ingredientInFormula" label="Ingredient in formula(%)"> </el-table-column>
      <el-table-column prop="purposeOfUse" label="Purpose of use"> </el-table-column>
      <el-table-column prop="templateUrl5" label="New RM" align="center" width="100"> </el-table-column>
    </el-table>
  </div>
</template>
  
<script>
export default {
  components: {},
  data() {
    return {
      listData: [],

      // 合并单元格
      column1Arr: [], // column1
      column1Index: 0, // column1索引

      column2Arr: [], // column2
      column2Index: 0, // column2索引

      column3Arr: [], // column3
      column3Index: 0, // column3索引

      column4Arr: [], // column4
      column4Index: 0, // column4索引

      column5Arr: [], // column5
      column5Index: 0, // column5索引

      column6Arr: [], // column6
      column6Index: 0, // column6索引

      column7Arr: [], // column7
      column7Index: 0, // column7索引
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  mounted() {
    
  },
  methods: {
    getList() {
      const prams = {
        taskId: this.taskId,
        formulaId: this.formulaId
      }
      this.$http({
        headers: {
          'Content-Type': 'application/json',
        },
        method: 'post',
        url: '/1123123123',
        data: prams,
      })
      .then(res=>{
        this.listData = res.data.records//给data中的变量赋值 把res.data.records换成自己
//的据接口 更改一下数据的字段 便可以直接使用了  
        this.mergeTable(this.listData)//合并单元格传入数据
      })
      //清空之前的数据
      this.column1Arr = []
      this.column1Index = 0
      this.column2Arr = []
      this.column2Index = 0
      this.column3Arr = []
      this.column3Index = 0
      this.column4Arr = []
      this.column4Index = 0
      this.column5Arr = []
      this.column5Index = 0
      this.column6Arr = []
      this.column6Index = 0
      this.column7Arr = []
      this.column7Index = 0
    },
    mergeTable(data) {
      if (data.length > 0) {
        for (var i = 0; i < data.length; i++) {
          if (i === 0) {
            // 第一行必须存在,以第一行为基准
            this.column1Arr.push(1) // column1
            this.column1Index = 0
            this.column2Arr.push(1) // column2
            this.column2Index = 0

            this.column3Arr.push(1) // column3
            this.column3Index = 0

            this.column4Arr.push(1) // column4
            this.column4Index = 0

            this.column5Arr.push(1) // column5
            this.column5Index = 0

            this.column6Arr.push(1) // column6
            this.column6Index = 0

            this.column7Arr.push(1) // column7
            this.column7Index = 0
          } else {
            // 判断当前元素与上一元素是否相同
            // column1
            if (data[i].checkResult === data[i - 1].checkResult) {
              this.column1Arr[this.column1Index] += 1
              this.column1Arr.push(0)
            } else {
              this.column1Arr.push(1)
              this.column1Index = i
            }

            // column2
              if (data[i].standardChineseName === data[i - 1].standardChineseName && data[i].checkResult === data[i - 1].checkResult) {
                this.column2Arr[this.column2Index] += 1
                this.column2Arr.push(0)
              } else {
                this.column2Arr.push(1)
                this.column2Index = i
              }

              // column3
              if (data[i].ingredientInciCtfaName === data[i - 1].ingredientInciCtfaName && data[i].checkResult === data[i - 1].checkResult) {
                this.column3Arr[this.column3Index] += 1
                this.column3Arr.push(0)
              } else {
                this.column3Arr.push(1)
                this.column3Index = i
              }

              // column4
              if (data[i].RMInFormula === data[i - 1].RMInFormula && data[i].checkResult === data[i - 1].checkResult) {
                this.column4Arr[this.column4Index] += 1
                this.column4Arr.push(0)
              } else {
                this.column4Arr.push(1)
                this.column4Index = i
              }

              // column5
              if (data[i].ingredientInRM === data[i - 1].ingredientInRM && data[i].checkResult === data[i - 1].checkResult) {
                this.column5Arr[this.column5Index] += 1
                this.column5Arr.push(0)
              } else {
                this.column5Arr.push(1)
                this.column5Index = i
              }

              // column6
              if (data[i].ingredientInFormula === data[i - 1].ingredientInFormula && data[i].checkResult === data[i - 1].checkResult) {
                this.column6Arr[this.column6Index] += 1
                this.column6Arr.push(0)
              } else {
                this.column6Arr.push(1)
                this.column6Index = i
              }

              // column7
              if (data[i].purposeOfUse === data[i - 1].purposeOfUse && data[i].checkResult === data[i - 1].checkResult) {
                this.column7Arr[this.column7Index] += 1
                this.column7Arr.push(0)
              } else {
                this.column7Arr.push(1)
                this.column7Index = i
              }
          }
        }
      }
    },

    handleSpanMethod({ rowIndex, columnIndex }) {
      if (columnIndex === 1) {
        // 第二列 column2
        const _row_1 = this.column1Arr[rowIndex]
        const _col_1 = _row_1 > 0 ? 1 : 0
        return {
          rowspan: _row_1,
          colspan: _col_1,
        }
      } 
      else if (columnIndex === 2) {
        // 第三列 column3
        const _row_2 = this.column2Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2,
        }
      } else if (columnIndex === 3) {
        // 第四列 column4
        const _row_3 = this.column3Arr[rowIndex]
        const _col_3 = _row_3 > 0 ? 1 : 0
        return {
          rowspan: _row_3,
          colspan: _col_3,
        }
      } else if (columnIndex === 4) {
        // 第五列 column5
        const _row_4 = this.column4Arr[rowIndex]
        const _col_4 = _row_4 > 0 ? 1 : 0
        return {
          rowspan: _row_4,
          colspan: _col_4,
        }
      } else if (columnIndex === 5) {
        // 第六列 column6
        const _row_5 = this.column5Arr[rowIndex]
        const _col_5 = _row_5 > 0 ? 1 : 0
        return {
          rowspan: _row_5,
          colspan: _col_5,
        }
      } else if (columnIndex === 6) {
        // 第七列 column7
        const _row_6 = this.column6Arr[rowIndex]
        const _col_6 = _row_6 > 0 ? 1 : 0
        return {
          rowspan: _row_6,
          colspan: _col_6,
        }
      } else if (columnIndex === 7) {
        // 第八列 column8
        const _row_7 = this.column7Arr[rowIndex]
        const _col_7 = _row_7 > 0 ? 1 : 0
        return {
          rowspan: _row_7,
          colspan: _col_7,
        }
      }
    }
}
</script>

总结 

到此这篇关于vue element ui表格相同数据合并单元格的文章就介绍到这了,更多相关element ui相同数据合并单元格内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue修改页面标签的方法示例

    Vue修改页面标签的方法示例

    vue项目有时候需要根据页面需要动态的去修改页面标题名称,本文就来介绍一下,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • vue如何向后台传递日期

    vue如何向后台传递日期

    这篇文章主要介绍了vue如何向后台传递日期,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • vuex的简单使用教程

    vuex的简单使用教程

    vuex是一个专门为vue.js设计的集中式状态管理架构。这篇文章主要介绍了vuex的简单使用,需要的朋友可以参考下
    2018-02-02
  • vue3使用flv.js播放推流视频的示例代码

    vue3使用flv.js播放推流视频的示例代码

    本文主要介绍了vue3使用flv.js播放推流视频的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • 详解vue-cli 本地开发mock数据使用方法

    详解vue-cli 本地开发mock数据使用方法

    这篇文章主要介绍了详解vue-cli 本地开发mock数据使用方法,如果后端接口尚未开发完成,前端开发一般使用mock数据。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • vue中计算属性(computed)、methods和watched之间的区别

    vue中计算属性(computed)、methods和watched之间的区别

    这篇文章主要给大家介绍了关于vue中计算属性(computed)、methods和watched之间区别的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
    2017-07-07
  • 详解vue中localStorage的使用方法

    详解vue中localStorage的使用方法

    这篇文章主要介绍了详解vue中localStorage的使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • 关于vue3.0使用axios报错问题

    关于vue3.0使用axios报错问题

    这篇文章主要介绍了vue3.0使用axios报错问题记录,vue-cli3.0安装插件的时候要注意区分vue-cli2.0的命令,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • 在Vue中实现二维码生成与扫描功能的方法

    在Vue中实现二维码生成与扫描功能的方法

    二维码是一种广泛应用于各种场合的编码方式,它可以将信息编码成一张二维图案,方便快捷地传递信息,在Vue.js中,我们可以使用一些库和组件来实现二维码的生成和扫描,本文将介绍如何在Vue中实现二维码的生成和扫描的方法
    2023-06-06
  • 没有搭建脚手架时vue组件的使用方式

    没有搭建脚手架时vue组件的使用方式

    这篇文章主要介绍了没有搭建脚手架时vue组件的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10

最新评论