ElementUi Table 全选反选常见问题

变量声明:

selectKeysUser: [],
selectUserObj: {},
<el-table
  ref="tableDataPerson"
  :height="330"
  :data="userTableData"
  border
  highlight-current-row
  row-key="id"
  @select="handleSelectionUserChange"
  @select-all="handleSelectUserAll"
  @selection-change="selectChangeUser"
  @row-click="clickChangeUserRow"
>

前端方法:

// 行选中函数  若有删除,若无添加
handleSelectionUserChange(val, row) {
  this.selectKeysUser = val
  if (this.selectUserObj[row.id]) {
    delete this.selectUserObj[row.id]
  } else {
    this.selectUserObj[row.id] = row
  }
  this.selectKeysUser = Object.keys(this.selectUserObj)
},
// 行选中函数  若有删除,若无添加
handleSelectUserRow(selection, row) {
  if (this.selectUserObj[row.id]) {
    delete this.selectUserObj[row.id]
  } else {
    this.selectUserObj[row.id] = row
  }
  this.selectKeysUser = Object.keys(this.selectUserObj)
},
// 全选函数  点击全选遍历当页数据若无添加,若是反选则删除(判断是否是全选还是反选)
handleSelectUserAll(selection) {
  this.userTableData.forEach((row, index) => {
    if (this.selectUserObj.hasOwnProperty(row.id)) {
      selection.length ? null : delete this.selectUserObj[row.id]
    } else {
      this.selectUserObj[row.id] = row
    }
  })
  this.selectKeysUser = Object.keys(this.selectUserObj)
  this.selectUserObj = selection
},
clickChangeUserRow(row) {
  this.clearSelectUser()
  this.$refs.tableDataPerson.toggleRowSelection(row, true)
  this.handleSelectUserRow(true, row)
},
selectChangeUser(selection) {
  const _this = this
  _this.selectRow = selection
  this.tzn = selection
  this.qur = []
  this.phone = []
  for (var i = 0; i < selection.length; i++) {
    this.qur.push(selection[i].realName)
    this.phone.push(selection[i].phone)
  }
},
clearSelectUser() {
  this.$refs.tableDataPerson.clearSelection()
  this.selectKeysUser = []
  this.selectUserObj = {}
},
// 记忆函数
memoryCheckedUser() {
  this.userTableData.forEach((row, index) => {
    const mt = this.$refs.tableDataPerson
    if (this.selectUserObj.hasOwnProperty(row.id)) {
      mt ? mt.toggleRowSelection(row, true) : undefined
    } else {
      mt ? mt.toggleRowSelection(row, false) : undefined
    }
  })
},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用 Vue.js 3 中的 v-model 指令和计算属性来实现全选反选功能,以下是示例代码: ```html <template> <div> <input type="checkbox" v-model="allChecked" @change="selectAll"> <table> <thead> <tr> <th> <input type="checkbox" v-model="checkAll"> </th> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr v-for="(item, index) in items" :key="index"> <td> <input type="checkbox" :value="item" v-model="checkedItems"> </td> <td>{{ item.name }}</td> <td>{{ item.age }}</td> </tr> </tbody> </table> </div> </template> <script> import { ref, computed } from 'vue'; export default { setup() { const items = ref([ { name: 'John', age: 25 }, { name: 'Mary', age: 30 }, { name: 'Tom', age: 20 } ]); const checkedItems = ref([]); const checkAll = ref(false); const allChecked = computed({ get() { return checkedItems.value.length === items.value.length; }, set(val) { checkedItems.value = val ? items.value.slice() : []; } }); function selectAll() { checkedItems.value = allChecked.value ? [] : items.value.slice(); } return { items, checkedItems, checkAll, allChecked, selectAll }; } }; </script> ``` 在上面的代码中,我们定义了一个 items 数组作为表格数据,checkedItems 数组用于存储选中的项,checkAll 表示全选复选框的状态,allChecked 是一个计算属性,用于判断全选复选框的状态,selectAll 方法用于实现全选反选功能。 在表格头部的复选框中,我们使用 v-model 指令将其与 checkAll 变量绑定,当 checkAll 发生变化时,表格中所有的复选框的状态也会跟着变化。在表格中的复选框中,我们使用 v-model 指令将其与 checkedItems 数组绑定,表示选中的项。 最后,我们使用 computed 函数来计算全选复选框的状态,并通过 selectAll 方法来实现全选反选功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值