当前位置:首页 > VUE

vue实现全选表格

2026-03-09 23:07:37VUE

实现全选表格的基本思路

在Vue中实现表格全选功能,通常需要结合表格的数据绑定和复选框的v-model指令。通过维护一个数组或对象来记录选中状态,可以轻松实现全选、反选等功能。

准备表格数据

假设有一个表格数据数组tableData,每个数据项包含idname等字段:

data() {
  return {
    tableData: [
      { id: 1, name: 'Item 1' },
      { id: 2, name: 'Item 2' },
      { id: 3, name: 'Item 3' }
    ],
    selectedItems: [] // 存储选中的项
  }
}

添加全选复选框

在表格的头部添加一个全选复选框,绑定到selectAll计算属性:

vue实现全选表格

<thead>
  <tr>
    <th>
      <input type="checkbox" v-model="selectAll" @change="toggleSelectAll">
    </th>
    <th>ID</th>
    <th>Name</th>
  </tr>
</thead>

实现全选逻辑

定义selectAll计算属性和toggleSelectAll方法:

computed: {
  selectAll: {
    get() {
      return this.selectedItems.length === this.tableData.length
    },
    set(value) {
      this.toggleSelectAll(value)
    }
  }
},
methods: {
  toggleSelectAll(checked) {
    this.selectedItems = checked ? [...this.tableData] : []
  }
}

绑定单个复选框

为表格的每一行添加复选框,绑定到selectedItems数组:

vue实现全选表格

<tbody>
  <tr v-for="item in tableData" :key="item.id">
    <td>
      <input 
        type="checkbox" 
        :value="item" 
        v-model="selectedItems"
      >
    </td>
    <td>{{ item.id }}</td>
    <td>{{ item.name }}</td>
  </tr>
</tbody>

优化性能(大数据量)

对于大数据量的表格,使用对象存储选中状态比数组更高效:

data() {
  return {
    selectedItems: {} // 改为对象存储
  }
},
methods: {
  toggleSelectAll(checked) {
    if (checked) {
      this.tableData.forEach(item => {
        this.$set(this.selectedItems, item.id, true)
      })
    } else {
      this.selectedItems = {}
    }
  }
}

对应的复选框绑定方式:

<input 
  type="checkbox" 
  :checked="selectedItems[item.id]" 
  @change="selectedItems[item.id] = $event.target.checked"
>

完整示例代码

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>
            <input type="checkbox" v-model="selectAll">
          </th>
          <th>ID</th>
          <th>Name</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in tableData" :key="item.id">
          <td>
            <input 
              type="checkbox" 
              :value="item" 
              v-model="selectedItems"
            >
          </td>
          <td>{{ item.id }}</td>
          <td>{{ item.name }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' }
      ],
      selectedItems: []
    }
  },
  computed: {
    selectAll: {
      get() {
        return this.selectedItems.length === this.tableData.length
      },
      set(value) {
        this.selectedItems = value ? [...this.tableData] : []
      }
    }
  }
}
</script>

标签: 全选表格
分享给朋友:

相关文章

css表格制作表格

css表格制作表格

CSS 表格制作方法 使用 CSS 创建表格可以通过多种方式实现,包括原生 HTML 表格元素结合 CSS 样式,或者使用纯 CSS 布局技术(如 Flexbox 或 Grid)模拟表格效果。以下是常…

vue实现表格拖动列宽

vue实现表格拖动列宽

实现表格列宽拖动的核心思路 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽逻辑,动态修改列宽。需要处理表头与表体的联动,确保拖动时视觉一致。 基础HTML结构 &…

css表格制作表格

css表格制作表格

CSS 表格制作方法 基础表格结构 使用HTML的<table>标签创建表格框架,搭配<tr>(行)、<td>(单元格)和<th>(表头)标签: &l…

vue element实现表格

vue element实现表格

Vue + Element UI 实现表格 Element UI 提供了强大的表格组件 el-table,结合 Vue 可以快速实现数据展示、排序、筛选等功能。 基础表格实现 安装 Element…

vue 实现表格单选

vue 实现表格单选

实现表格单选的基本思路 在Vue中实现表格单选功能,通常需要结合表格组件(如el-table)和数据绑定。核心逻辑是通过点击事件更新当前选中行的唯一标识(如id),并通过:class或:style绑定…

vue实现分组表格

vue实现分组表格

Vue 实现分组表格的方法 使用 v-for 嵌套循环实现分组 通过嵌套的 v-for 循环可以轻松实现分组表格。外层循环遍历分组数据,内层循环遍历每组中的具体项。 <template>…