el-table使用span-method合并行

本文详细介绍了在Vue.js中如何使用`el-table`组件结合`span-method`属性实现表格行的动态合并。通过遍历数据,比较相邻行的特定字段,设置合并标记并计算合并行数。实例展示了在表格中根据`levelName1`、`levelName2`、`levelName3`和`weight`字段进行行合并的方法,适用于数据展示优化和表格美化。
摘要由CSDN通过智能技术生成

el-table使用span-method合并行

1、html页面

<el-table height="650" :data="skuListInfo" ref="tableData" border :span-method="objectSpanMethod" :header-cell-style="{background:'#deebff',color:'#606266',fontWeight:'bold',textAlign:'center'}" highlight-current-row>
	<el-table-column prop="deptName" align="center" label="序号" min-width="30">
		<template slot-scope='scope'>
			{{scope.row.tableSort}}
	    </template>
	</el-table-column>
	<el-table-column prop="levelName1" align="center" label="重点工作" min-width="80">
		<template slot-scope='scope'>
		{{scope.row.levelName1}}
		</template>
    </el-table-column>
    <el-table-column prop="levelName2" align="center" label="主要项目" min-width="200">
    	<template slot-scope='scope'>
    	{{scope.row.levelName2}}
    	</template>
   	</el-table-column>
   	<el-table-column prop="levelName3" align="center" label="具体内容" min-width="180">
	   	<template slot-scope='scope'>
	   	{{scope.row.levelName3 ? scope.row.levelName3 : '\\'}}
	   	</template>
   	</el-table-column>
   	<el-table-column prop="unitVal" align="center" label="单位" min-width="50">
   		<template slot-scope='scope'>
   		{{scope.row.unitVal}}
   		</template>
   	</el-table-column>
   	<el-table-column prop="dataTotal" align="center" label="数据" min-width="90">
   		<template slot-scope='scope'>
   			<el-input v-model="scope.row.dataTotal" @input="dataTotalChange(scope.row, 'dataTotal', 'targetTotal', scope.$index)" @keyup.native="proving($event)" :disabled="!isEdit"></el-input>
   		</template>
   	</el-table-column>
   	<el-table-column prop="dataTotal" align="center" label="大屏数据" min-width="70" v-if="isAdmin">
   		<template slot-scope='scope'>
   			<el-input v-model="scope.row.bigDataTotal" @input="dataTotalChange(scope.row, 'bigDataTotal', 'bigTargetTotal', scope.$index)" @keyup.native="proving($event)" :disabled="!isAdmin"></el-input>
   		</template>
   	</el-table-column>
   	<el-table-column prop="weight" align="center" label="权重" min-width="60" v-if="isAdmin">
   		<template slot-scope='scope'>
   			<div >{{scope.row.weight ? (scope.row.weight + '%') : '\\'}}</div>
   		</template>
   	</el-table-column>
   	<el-table-column prop="baseTotal" align="center" label="基数" min-width="60" v-if="isAdmin">
   		<template slot-scope='scope'>
   			<div >{{scope.row.baseTotal}}</div>
   		</template>
   	</el-table-column>
   	<el-table-column prop="targetTotal" align="center" label="指标数" min-width="60" v-if="isAdmin">
   		<template slot-scope='scope'>
   			<div>{{scope.row.targetTotal ? scope.row.targetTotal + '%' : ''}}</div>
   		</template>
   	</el-table-column>
   	<el-table-column prop="bigTargetTotal" align="center" label="大屏指标数" min-width="60" v-if="isAdmin">
   		<template slot-scope='scope'>
   			<div>{{scope.row.bigTargetTotal ? scope.row.bigTargetTotal + '%' : ''}}</div>
   		</template>
   	</el-table-column>
   	<el-table-column prop="caliberContent" align="left" label="填报口径说明" min-width="150">
   		<template slot-scope='scope'>
   			<span v-html="scope.row.caliberContent">{{scope.row.caliberContent}}</span>
   		</template>
   	</el-table-column>
</el-table>

2、javascript部分

<script>
export default {
	data() {
		return {
			skuListInfo: [
				{
					baseTotal: null
					bigDataTotal: null
					bigTargetTotal: null
					dataTotal: null
					id: "aa790401jk7207041"
					levelName1: "组织党员干部自学"
					levelName2: "参加学习党员人数"
					levelName3: ""
					levelName4: ""
					targetTotal: null
					unitVal: "人"
					weight: 40
				},
				{
					baseTotal: null
					bigDataTotal: null
					bigTargetTotal: null
					dataTotal: null
					id: "aa790401jk7207042"
					levelName1: "组织党员干部自学"
					levelName2: "参加学习县处级以上党员干部人数"
					levelName3: ""
					levelName4: ""
					targetTotal: null
					unitVal: "人"
					weight: 40
				},
				{
					baseTotal: null
					bigDataTotal: null
					bigTargetTotal: null
					dataTotal: null
					id: "aa790401jk7207043"
					levelName1: "组织党员干部自学"
					levelName2: "所属基层党组织数量"
					levelName3: ""
					levelName4: ""
					targetTotal: null
					unitVal: "人"
					weight: 40
				},
				{
					baseTotal: null
					bigDataTotal: null
					bigTargetTotal: null
					dataTotal: null
					id: "aa79040hjk7207123"
					levelName1: "组织党员干部自学"
					levelName2: "发放教材、笔记本等资料数量"
					levelName3: ""
					levelName4: ""
					targetTotal: null
					unitVal: "人"
					weight: 40
				},
				{
					baseTotal: null
					bigDataTotal: null
					bigTargetTotal: null
					dataTotal: null
					id: "aa790401jk7207044"
					levelName1: "开展专题学习"
					levelName2: "党委(党组)理论学习中心组学习情况"
					levelName3: "本级专题学习次数"
					levelName4: ""
					targetTotal: null
					unitVal: "人"
					weight: 40
				},
				{
					baseTotal: null
					bigDataTotal: null
					bigTargetTotal: null
					dataTotal: null
					id: "aa790401jk7207045"
					levelName1: "开展专题学习"
					levelName2: "党委(党组)理论学习中心组学习情况"
					levelName3: "下属各级专题学习次数"
					levelName4: ""
					targetTotal: null
					unitVal: "人"
					weight: 40
				},
				{
					baseTotal: null
					bigDataTotal: null
					bigTargetTotal: null
					dataTotal: null
					id: "aa790401jk7207046"
					levelName1: "开展专题学习"
					levelName2: "基层党组织开展学习党史主题党日活动情况"
					levelName3: "开展主题党日活动的基层党组织数量"
					levelName4: ""
					targetTotal: null
					unitVal: "人"
					weight: 40
				},
				{
					baseTotal: null
					bigDataTotal: null
					bigTargetTotal: null
					dataTotal: null
					id: "aa790401jk7207047"
					levelName1: "开展专题学习"
					levelName2: "基层党组织开展学习党史主题党日活动情况"
					levelName3: "开展主题党日活动的数量"
					levelName4: ""
					targetTotal: null
					unitVal: "人"
					weight: 40
				},
				{
					baseTotal: null
					bigDataTotal: null
					bigTargetTotal: null
					dataTotal: null
					id: "aa790401jk7207048"
					levelName1: "开展专题学习"
					levelName2: "领导干部讲专题党课数量"
					levelName3: "市厅级领导干部讲专题党课数量"
					levelName4: ""
					targetTotal: null
					unitVal: "人"
					weight: 40
				},
				{
					baseTotal: null
					bigDataTotal: null
					bigTargetTotal: null
					dataTotal: null
					id: "aa790401jk7207049"
					levelName1: "开展专题学习"
					levelName2: "领导干部讲专题党课数量"
					levelName3: "县处级领导干部讲专题党课数量"
					levelName4: ""
					targetTotal: null
					unitVal: "人"
					weight: 40
				},
				{
					baseTotal: null
					bigDataTotal: null
					bigTargetTotal: null
					dataTotal: null
					id: "aa790401jk7207050"
					levelName1: "开展专题学习"
					levelName2: "领导干部讲专题党课数量"
					levelName3: "基层党组织书记讲专题党课数量"
					levelName4: ""
					targetTotal: null
					unitVal: "人"
					weight: 40
				}
			],
			typeNameArr: [],
	      	typeNamePos: 0,
	      	storeArr: [],
	      	storePos: 0,
	      	feeArr: [],
	      	feePos: 0,
	      	weightArr: [],
	      	weightPos: 0
		}
	},
	mounted() {
		this.merage()
	},
	methods: {
	  merageInit() {
      this.typeNameArr = []
      this.typeNamePos = 0
      this.storeArr = []
      this.storePos = 0
      this.feeArr = []
      this.feePos = 0
      this.weightArr = []
      this.weightPos = 0
    },
    merage() {
      this.merageInit()
      this.tableSort = 1
      for (let i = 0; i < this.skuListInfo.length; i += 1) {
        if (i === 0) {
          // 第一行必须存在
          this.typeNameArr.push(1)
          this.typeNamePos = 0
          this.storeArr.push(1)
          this.storePos = 0
          this.feeArr.push(1)
          this.feePos = 0
          this.weightArr.push(1)
          this.weightPos = 0
          this.skuListInfo[i].tableSort = this.tableSort
        } else {
          // 判断当前元素与上一个元素是否相同,eg:this.typeNamePos 是 this.typeNameArr序号
          // 第一二列
          // eslint-disable-next-line no-lonely-if
          if (this.skuListInfo[i].levelName1 === this.skuListInfo[i - 1].levelName1) {
            this.typeNameArr[this.typeNamePos] += 1
            this.skuListInfo[i].tableSort = this.tableSort
            this.typeNameArr.push(0)
          } else {
            this.tableSort += 1
            this.skuListInfo[i].tableSort = this.tableSort
            this.typeNameArr.push(1)
            this.typeNamePos = i
          }
          // 第三列
          if (this.skuListInfo[i].levelName2 === this.skuListInfo[i - 1].levelName2 && this.skuListInfo[i].levelName1 ===
              this.skuListInfo[i - 1].levelName1) {
            this.storeArr[this.storePos] += 1
            this.storeArr.push(0)
          } else {
            this.storeArr.push(1)
            this.storePos = i
          }
          // 第四列
          if (this.skuListInfo[i].levelName3 === this.skuListInfo[i - 1].levelName3 && this.skuListInfo[i].levelName2 ===
              this.skuListInfo[i - 1].levelName2 && this.skuListInfo[i].levelName1 ===
              this.skuListInfo[i - 1].levelName1) {
            this.feeArr[this.feePos] += 1
            this.feeArr.push(0)
          } else {
            this.feeArr.push(1)
            this.feePos = i
          }
          // 第七列
          if (this.skuListInfo[i].weight && this.skuListInfo[i].weight === this.skuListInfo[i - 1].weight) {
            if ((this.skuListInfo[i].levelName1 === this.skuListInfo[i - 1].levelName1) && (this.skuListInfo[i].levelName1 === '组织专题培训' || this.skuListInfo[i].levelName1 === '组织专题宣讲' || this.skuListInfo[i].levelName1 === '开展“我为群众办实事”实践活动' || this.skuListInfo[i].levelName1 === '流动党员学习教育')) {
              this.weightArr[this.weightPos] += 1
              this.weightArr.push(0)
            } else if (this.skuListInfo[i].levelName3) {
              if (this.skuListInfo[i].levelName2 === this.skuListInfo[i - 1].levelName2) {
                this.weightArr[this.weightPos] += 1
                this.weightArr.push(0)
              } else {
                this.weightArr.push(1)
                this.weightPos = i
              }
            } else {
              this.weightArr.push(1)
              this.weightPos = i
            }
          } else {
            this.weightArr.push(1)
            this.weightPos = i
          }
        }
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      const lastColumnIndex = this.isAdmin ? 11 : 6
      if (columnIndex === 0 || columnIndex === 1 || columnIndex === lastColumnIndex) {
        if (this.skuListInfo.length - rowIndex === 1) {
          return [0, 0]
        }
        // 第一二列的合并方法
        const row1 = this.typeNameArr[rowIndex]
        const col1 = row1 > 0 ? 1 : 0 // 如果被合并了row = 0; 则他这个列需要取消
        return {
          rowspan: row1,
          colspan: col1
        }
      } else if (columnIndex === 2) {
        if (this.skuListInfo.length - rowIndex === 1) {
          return [1, 0]
        }
        // 第三列的合并方法
        const row2 = this.storeArr[rowIndex]
        const col2 = row2 > 0 ? 1 : 0 // 如果被合并了row = 0; 则他这个列需要取消
        return {
          rowspan: row2,
          colspan: col2
        }
      } else if (columnIndex === 3) {
        // 第四列的合并方法
        const row3 = this.feeArr[rowIndex]
        const col3 = row3 > 0 ? 1 : 0 // 如果被合并了row = 0; 则他这个列需要取消
        return {
          rowspan: row3,
          colspan: col3
        }
      } else if (columnIndex === 7 || columnIndex === 8 || columnIndex === 9 || columnIndex === 10) {
        const row4 = this.weightArr[rowIndex]
        const col4 = row4 > 0 ? 1 : 0
        return {
          rowspan: row4,
          colspan: col4
        }
      }
    },
}
}
</script>

3、效果图
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值