自定义合并el-table单元格,单元格错乱问题

自定义合并el-table单元格,单元格错乱问题

在使用element-ui表格的时候,需要自定义合并,出现了问题,如下图:
在这里插入图片描述
单元格的顺序发生错乱

接口返回的需要在表格中展示的数据
在这里插入图片描述

    // 表格列合并
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      
      const length = [] // 需要合并的长度集合
      let rowNum = '' // 需要合并的行数
      let index = ''  // 合并行的index
      // 这里注意要先确定每次需要合并的长度,才能往下进行
      // 我这里【productList的长度】就是每次需要合并的长度
      this.form.sales.forEach(element => {
        // 遍历sales,取出productList的长度 push 
        length.push(element.productList.length)
      })
      // length = [2,7]
      // 第一次从rowIndex = 0开始合并,合并2行,
      //第二次从rowIndex = 2开始合并,合并7行
      //第三次从rowindex = 9,依次类推
      // 发现每次合并对应的rowIndex 就是前面值相加的和,对length求和与rowIndex进行比较
      length.reduce((pre,curr) => {
        if(columnIndex === 0 && rowIndex === pre){
          rowNum = curr
          index = pre
        }
        return pre + curr
      },0)
      if(columnIndex === 0){
        if(index === rowIndex){
          return [rowNum,1]
        }else{
          // 防止单元格顺序错乱,必须加上
          return [0,0]; // [rowspan,colspan]
        }
      }
    },
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-table单元格嵌入el-input可以通过使用插槽来实现。在el-table-column中使用scoped slot,将单元格的数据传递给插槽,然后在插槽中使用el-input来编辑数据。具体步骤如下: 1. 在el-table-column中使用scoped slot,将单元格的数据传递给插槽。 2. 在插槽中使用el-input来编辑数据。 3. 在el-input中使用v-model绑定数据,使得编辑后的数据能够同步到数据源中。 举个例子,假设我们要在el-table中的某一列中嵌入el-input来编辑数据,可以按照以下步骤进行操作: 1. 在el-table-column中使用scoped slot,将单元格的数据传递给插槽。 ``` <el-table-column label="姓名"> <template slot-scope="scope"> <span>{{ scope.row.name }}</span> <template v-if="scope.row.editable"> <el-input v-model="scope.row.name"></el-input> </template> </template> </el-table-column> ``` 2. 在插槽中使用el-input来编辑数据。 ``` <el-table-column label="姓名"> <template slot-scope="scope"> <span>{{ scope.row.name }}</span> <template v-if="scope.row.editable"> <el-input v-model="scope.row.name"></el-input> </template> </template> </el-table-column> ``` 3. 在el-input中使用v-model绑定数据,使得编辑后的数据能够同步到数据源中。 ``` <el-table-column label="姓名"> <template slot-scope="scope"> <span>{{ scope.row.name }}</span> <template v-if="scope.row.editable"> <el-input v-model="scope.row.name"></el-input> </template> </template> </el-table-column> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值