当前位置:首页 > VUE

Vue中table实现CheckBox

2026-01-21 18:12:04VUE

Vue中实现表格复选框功能

在Vue中为表格添加复选框功能可以通过多种方式实现,以下是几种常见方法:

使用v-model绑定选中状态

为表格的每一行数据添加一个选中状态字段,通过v-model绑定复选框:

Vue中table实现CheckBox

<template>
  <table>
    <thead>
      <tr>
        <th><input type="checkbox" v-model="selectAll"></th>
        <th>姓名</th>
        <th>年龄</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item, index) in tableData" :key="index">
        <td><input type="checkbox" v-model="item.selected"></td>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 25, selected: false },
        { name: '李四', age: 30, selected: false },
        { name: '王五', age: 28, selected: false }
      ],
      selectAll: false
    }
  },
  watch: {
    selectAll(newVal) {
      this.tableData.forEach(item => {
        item.selected = newVal
      })
    }
  }
}
</script>

使用数组存储选中项ID

对于大数据量表格,可以采用存储选中项ID的方式提高性能:

<template>
  <table>
    <thead>
      <tr>
        <th><input type="checkbox" 
                  :checked="selectedItems.length === tableData.length"
                  @change="toggleSelectAll"></th>
        <th>ID</th>
        <th>姓名</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in tableData" :key="item.id">
        <td><input type="checkbox" 
                  :checked="selectedItems.includes(item.id)"
                  @change="toggleSelect(item.id)"></td>
        <td>{{ item.id }}</td>
        <td>{{ item.name }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '张三' },
        { id: 2, name: '李四' },
        { id: 3, name: '王五' }
      ],
      selectedItems: []
    }
  },
  methods: {
    toggleSelect(id) {
      const index = this.selectedItems.indexOf(id)
      if (index > -1) {
        this.selectedItems.splice(index, 1)
      } else {
        this.selectedItems.push(id)
      }
    },
    toggleSelectAll(e) {
      if (e.target.checked) {
        this.selectedItems = this.tableData.map(item => item.id)
      } else {
        this.selectedItems = []
      }
    }
  }
}
</script>

使用第三方组件库

Element UI等流行UI库提供了现成的表格复选框功能:

Vue中table实现CheckBox

<template>
  <el-table
    :data="tableData"
    @selection-change="handleSelectionChange"
    style="width: 100%">
    <el-table-column
      type="selection"
      width="55">
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="180">
    </el-table-column>
    <el-table-column
      prop="age"
      label="年龄">
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [{
        name: '张三',
        age: 25
      }, {
        name: '李四',
        age: 30
      }],
      multipleSelection: []
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>

实现跨页选择功能

对于分页表格,需要额外处理跨页选择:

methods: {
  toggleSelect(item) {
    const id = item.id
    if (this.selectedMap[id]) {
      delete this.selectedMap[id]
    } else {
      this.selectedMap[id] = true
    }
    this.selectedItems = Object.keys(this.selectedMap)
  },
  isSelected(id) {
    return this.selectedMap[id] || false
  }
}

性能优化建议

对于大型数据集,考虑使用虚拟滚动技术避免渲染所有行。可以采用以下策略:

  1. 使用对象而非数组存储选中状态,提升查找效率
  2. 实现懒加载和分页功能
  3. 避免在模板中使用复杂计算属性
  4. 考虑使用Vue的v-once指令优化静态内容

以上方法可根据具体项目需求选择或组合使用,实现灵活高效的表格复选框功能。

标签: Vuetable
分享给朋友:

相关文章

Vue gitbook 实现

Vue gitbook 实现

Vue 与 GitBook 集成实现文档站点 将 Vue 集成到 GitBook 中可以实现动态、交互式的文档站点。GitBook 本身基于 Markdown,但通过插件或自定义构建流程可引入 Vue…

Vue 实现登出

Vue 实现登出

Vue 实现登出的方法 清除用户状态 在Vue应用中,登出通常需要清除用户的登录状态。可以通过清除Vuex中的用户数据或本地存储的token来实现。 // 在Vuex的actions中 logou…

Vue实现lagger页面

Vue实现lagger页面

Vue实现懒加载页面的方法 懒加载(Lazy Loading)是一种优化技术,用于延迟加载页面或组件,直到它们真正需要显示时才加载。以下是几种在Vue中实现懒加载页面的常见方法: 使用Vue Rou…

elementui table

elementui table

ElementUI Table 基本用法 ElementUI 的 Table 组件用于展示结构化数据,支持排序、筛选、分页等功能。以下是一个基础示例: <template> &…

Vue实现思路

Vue实现思路

Vue 实现思路 Vue 是一个渐进式 JavaScript 框架,适用于构建用户界面。其核心思想是数据驱动和组件化开发。以下是 Vue 实现的主要思路和方法: 数据驱动与响应式 Vue 通过数据…

Vue数据依赖实现

Vue数据依赖实现

Vue数据依赖实现原理 Vue的数据依赖实现基于响应式系统,核心是通过Object.defineProperty或Proxy拦截数据变化,结合发布-订阅模式实现依赖收集和更新触发。 核心机制 依赖…