当前位置:首页 > VUE

vue实现表格取消选择

2026-02-21 19:36:00VUE

Vue 实现表格取消选择的方法

使用 v-model 和 computed 属性

在表格中绑定选中状态,通过计算属性管理选中项。取消选择时,直接操作绑定的数组。

<template>
  <table>
    <tr v-for="item in items" :key="item.id">
      <td>
        <input 
          type="checkbox" 
          v-model="selectedItems" 
          :value="item.id"
        >
      </td>
      <td>{{ item.name }}</td>
    </tr>
    <button @click="clearSelection">取消选择</button>
  </table>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ],
      selectedItems: []
    }
  },
  methods: {
    clearSelection() {
      this.selectedItems = []
    }
  }
}
</script>

使用 ref 和 DOM 操作

通过 ref 获取所有复选框元素,手动取消选中状态。

<template>
  <table>
    <tr v-for="item in items" :key="item.id">
      <td>
        <input 
          type="checkbox" 
          :ref="`checkbox-${item.id}`"
          @change="handleSelect(item.id)"
        >
      </td>
      <td>{{ item.name }}</td>
    </tr>
    <button @click="clearSelection">取消选择</button>
  </table>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ],
      selectedItems: []
    }
  },
  methods: {
    handleSelect(id) {
      const index = this.selectedItems.indexOf(id)
      if (index === -1) {
        this.selectedItems.push(id)
      } else {
        this.selectedItems.splice(index, 1)
      }
    },
    clearSelection() {
      this.selectedItems = []
      this.items.forEach(item => {
        this.$refs[`checkbox-${item.id}`][0].checked = false
      })
    }
  }
}
</script>

使用事件代理

在表格外层元素上监听点击事件,通过事件委托处理取消选择逻辑。

<template>
  <div @click="handleTableClick">
    <table ref="table">
      <tr v-for="item in items" :key="item.id">
        <td>
          <input 
            type="checkbox" 
            :data-id="item.id"
            :checked="selectedItems.includes(item.id)"
          >
        </td>
        <td>{{ item.name }}</td>
      </tr>
    </table>
    <button @click="clearSelection">取消选择</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ],
      selectedItems: []
    }
  },
  methods: {
    handleTableClick(e) {
      if (e.target.type === 'checkbox') {
        const id = Number(e.target.dataset.id)
        this.toggleSelection(id)
      }
    },
    toggleSelection(id) {
      const index = this.selectedItems.indexOf(id)
      if (index === -1) {
        this.selectedItems.push(id)
      } else {
        this.selectedItems.splice(index, 1)
      }
    },
    clearSelection() {
      this.selectedItems = []
      const checkboxes = this.$refs.table.querySelectorAll('input[type="checkbox"]')
      checkboxes.forEach(checkbox => {
        checkbox.checked = false
      })
    }
  }
}
</script>

使用第三方表格组件

如果使用 Element UI 等组件库,可以利用其内置方法实现取消选择。

vue实现表格取消选择

<template>
  <el-table
    ref="multipleTable"
    :data="items"
    @selection-change="handleSelectionChange"
  >
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column prop="name" label="Name"></el-table-column>
  </el-table>
  <el-button @click="clearSelection">取消选择</el-button>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ],
      selectedItems: []
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.selectedItems = val
    },
    clearSelection() {
      this.$refs.multipleTable.clearSelection()
    }
  }
}
</script>

标签: 表格vue
分享给朋友:

相关文章

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…

vue监听实现

vue监听实现

Vue 监听实现方法 在 Vue 中可以通过多种方式实现监听数据变化或事件触发,以下是常见的几种方法: 使用 watch 选项 watch 是 Vue 提供的核心功能之一,用于观察和响应 Vue 实…