element 合并列

https://blog.csdn.net/qq_41442428/article/details/115182979

<template>
  <div class="GlobalSituation">
    <el-table class="control-table master-table" :data="tableList.tableData" style="width: 100%;" border :span-method="spanMethod" :row-class-name="tableRowClassName">
      <el-table-column label="序号" width="50" align="center">
        <template slot-scope="scope">
          <div class="table-index">{{ (scope.row.group) + 1 }}</div>
        </template>
      </el-table-column>
      <el-table-column v-for="(val,index) in tableList.tableHeader" :key="index" :prop="val.displayName" :label="val.name" align="center">
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  name: 'GlobalSituation',
  components: {},
  directives: {},
  filters: {},
  mixins: [],
  // props: ['tableList'],
  data() {
    return {
      tableList: {
        tableHeader: [{ name: '阶段', displayName: 'stage' }, { name: '名字', displayName: 'plotcode' }, { name: '代码', displayName: 'landCode' }, { name: '性质', displayName: 'landUse' }, { name: '面积(m2)', displayName: 'area' }, { name: '概率', displayName: 'volumeRate' }, { name: '密度(%)', displayName: 'buildingDensity' }, { name: '成功率(%)', displayName: 'greenRate' }, { name: '高度(m)', displayName: 'buildingHeight' }],
        tableData: [
          {
            id: 1,
            stage: '调整前',
            plotcode: 'A01-01',
            landCode: 'R2',
            landUse: '王小虎',
            area: 76017.86,
            volumeRate: 2.5,
            buildingDensity: 35,
            greenRate: 35,
            buildingHeight: 80
          },
          {
            id: 1,
            stage: '调整前',
            plotcode: 'A01-01',
            landCode: 'R2',
            landUse: '王小虎',
            area: 76017.86,
            volumeRate: 2.5,
            buildingDensity: 35,
            greenRate: 35,
            buildingHeight: 80
          },
          {
            id: 1,
            stage: '调整后',
            plotcode: 'A02-04',
            landCode: 'R2',
            landUse: '王小虎',
            area: 76017.86,
            volumeRate: 2.5,
            buildingDensity: 35,
            greenRate: 35,
            buildingHeight: 80
          }, {
            id: 2,
            stage: '调整前',
            plotcode: 'A01-03',
            landCode: 'G2',
            landUse: '王小虎',
            area: 76017.86,
            volumeRate: 2.5,
            buildingDensity: 35,
            greenRate: 35,
            buildingHeight: 80
          }, {
            id: 2,
            stage: '调整前',
            plotcode: 'A03-03',
            landCode: 'G2',
            landUse: '王小虎',
            area: 76017.86,
            volumeRate: 2.5,
            buildingDensity: 35,
            greenRate: 35,
            buildingHeight: 80
          }, {
            id: 2,
            stage: '调整前',
            plotcode: 'A03-06',
            landCode: 'G1',
            landUse: '王小虎',
            area: 76017.86,
            volumeRate: 2.5,
            buildingDensity: 35,
            greenRate: 35,
            buildingHeight: 80
          }, {
            id: 2,
            stage: '调整后',
            plotcode: 'A02-06',
            landCode: 'G2',
            landUse: '王小虎',
            area: 76017.86,
            volumeRate: 2.5,
            buildingDensity: 35,
            greenRate: 35,
            buildingHeight: 80
          }, {
            id: 2,
            stage: '调整后',
            plotcode: 'A02-06',
            landCode: 'G2',
            landUse: '王小虎',
            area: 76017.86,
            volumeRate: 2.5,
            buildingDensity: 35,
            greenRate: 35,
            buildingHeight: 80
          },
          {
            id: 2,
            stage: '调整后',
            plotcode: 'A0222-06',
            landCode: 'G2',
            landUse: '王小虎',
            area: 76017.86,
            volumeRate: 2.5,
            buildingDensity: 35,
            greenRate: 35,
            buildingHeight: 80
          }
        ]
      },
      spanArr: [],
      position: 0,
      needSpan: ['stage', 'id'], // 需要合并的属性
      spanArrObj: {
        stage: [],
        id: []
      }
    }
  },
  created() {
    this.getSpanArr()
  },
  watch: {
  },
  mounted() {
  },
  methods: {
    // 表格行样式
    tableRowClassName({ row, rowIndex }) {
      if (row.stage === '调整前') {
        return 'highlight-row'
      } else {
        return ''
      }
    },
    getSpanArr() {
      this.needSpan.forEach(item => {
        this.rowspan(item)
      })
    },
    // 获取合并行参数(数组)
    rowspan(prop) {
      this.tableList.tableData.forEach((item, index) => {
        if (index === 0) {
          this.spanArrObj[prop].push(1)
          this.position = 0
        } else {
          if (this.tableList.tableData[index][prop] === this.tableList.tableData[index - 1][prop]) {
            this.spanArrObj[prop][this.position] += 1
            this.spanArrObj[prop].push(0)
          } else {
            this.spanArrObj[prop].push(1)
            this.position = index
          }
        }

        // 获取表格的序号
        if (index === 0) {
          item.group = index //先设置组号赋值为0 也就是序号
        } else {
          // stage字段相同进行合并
          if (item.id === this.tableList.tableData[index - 1].id) {
            item.group = this.tableList.tableData[index - 1].group //如果一样 将组号改为和上一个数据相同
          } else {
            item.group = this.tableList.tableData[index - 1].group + 1 //如果不一样 将组号设置为上一个数据的组号加1
          }
        }
      })
      console.log(this.spanArrObj)
    },


    // 合并行方法
    spanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this.spanArrObj['id'][rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
      if (columnIndex === 1) {
        const _row = this.spanArrObj['stage'][rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    }
  }

}
</script>

<style lang="scss" scoped>
.GlobalSituation {
  /deep/.el-table .success-row {
    background: #f5f7fa;
  }
  /deep/.el-table--border td:first-child .cell,
  /deep/.el-table--border th:first-child .cell {
    padding-left: 10px;
  }
  .el-table /deep/ .highlight-row {
    background-color: #f1fafd;
  }
}
</style>



自己写的


<el-table
    :data="tableData"
    border
    :span-method="objectSpanMethod"
    height="calc(100vh - 315px)"
    style="width: 100%">
</el-table>

data(){
	  spanArr: [],
      position: 0,
      needSpan: ['role'], // 需要合并的属性
      spanArrObj: {
       role: []
      }
   }

created(){
    this.getSpanArr()
},
methods:{
getSpanArr() {
      this.needSpan.forEach(item => {
        this.rowspan(item)
      })
    },
// 获取合并行参数(数组)
    rowspan(prop) {
      this.tableData.forEach((item, index) => {
        if (index === 0) {
          this.spanArrObj[prop].push(1)
          this.position = 0
        } else {
          if (this.tableData[index][prop] === this.tableData[index - 1][prop]) {
            this.spanArrObj[prop][this.position] += 1
            this.spanArrObj[prop].push(0)
          } else {
            this.spanArrObj[prop].push(1)
            this.position = index
          }
        }
      })
      console.log(this.spanArrObj)
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }){
        
        if (columnIndex === 0) {
        const _row = this.spanArrObj['role'][rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值