element table组件自定义索引时,selection-change方法检测不到当选择项发生变化的值问题?

问题解释:

在使用Element 的Table组件时,如果发现自定义索引序号能够正常显示,但是在使用selection-change方法时无法检测到选中项,这可能是因为自定义索引序号与Table组件内部维护的选中状态不同步。

解决方法:

  1. 确保你在渲染表格行时使用正确的索引。

  2. 如果你使用了v-for来渲染表格行,确保你绑定的:key是唯一的,并且不会导致不必要的渲染问题。

  3. 确保你的自定义索引序号与Table组件的选中状态同步。如果你使用了自定义索引,你可能需要在内部维护一个映射,将自定义索引映射到Table组件的实际数据数组的索引上。

附上代码:

<template>
  <div>
    <el-table
        ref="multipleTable"
        :data="tableData"
        :fit="true"
        empty-text="暂无数据"
        @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="240">
        <template #default="scope">
          <el-button type="danger" plain>
            删除
          </el-button>
          <el-button type="primary" plain>
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

row-key:行数据的 Key,用来优化 Table 的渲染。

<template>
  <div>
    <!--
      :row-key属性确保了每行数据的唯一性,使Table组件能够正确识别每一行数据
      :row-key属性对于Table组件内部维护选中状态是必要的
      handleSelectionChange方法用于响应选中项的变化
    -->
    <el-table
        ref="multipleTable"
        :data="tableData"
        :fit="true"
        row-key="id"
        empty-text="暂无数据"
        @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="240">
        <template #default="scope">
          <el-button type="danger" plain>
            删除
          </el-button>
          <el-button type="primary" plain>
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "index",

  data () {
    return {
      multipleSelection: [],
      tableData: [
        // 数据数组
      ]
    }
  },

  methods: {
    // 当选择项发生变化时会触发该事件
    handleSelectionChange(val) {
      this.multipleSelection = val
      // console.log('查看选项变化', this.multipleSelection)
    }
  }
}
</script>

<style scoped lang="scss">
// 样式
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值