当前位置:首页 > VUE

vue实现表格复选

2026-02-10 19:39:32VUE

Vue实现表格复选的步骤

基础实现

使用v-model绑定复选框和数组,通过v-for渲染表格行。

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

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' }
      ],
      selectedItems: [],
      selectAll: false
    }
  },
  methods: {
    toggleAll() {
      this.selectedItems = this.selectAll ? this.tableData.map(item => item.id) : []
    }
  }
}
</script>

性能优化方案

对于大数据量表格,使用虚拟滚动或分页加载。

<template>
  <div class="virtual-scroll" @scroll="handleScroll">
    <div class="scroll-content" :style="{ height: totalHeight + 'px' }">
      <div v-for="(item, index) in visibleData" :key="item.id" :style="{ transform: `translateY(${item.offset}px)` }">
        <input type="checkbox" v-model="selectedItems" :value="item.id">
        {{ item.name }}
      </div>
    </div>
  </div>
</template>

高级功能实现

添加行点击选中、Shift多选等增强交互。

methods: {
  handleRowClick(item, event) {
    if (event.shiftKey && this.lastSelected) {
      const start = this.tableData.findIndex(i => i.id === this.lastSelected)
      const end = this.tableData.findIndex(i => i.id === item.id)
      const range = this.tableData.slice(Math.min(start, end), Math.max(start, end) + 1)
      range.forEach(row => {
        if (!this.selectedItems.includes(row.id)) {
          this.selectedItems.push(row.id)
        }
      })
    } else {
      const index = this.selectedItems.indexOf(item.id)
      if (index === -1) {
        this.selectedItems.push(item.id)
      } else {
        this.selectedItems.splice(index, 1)
      }
    }
    this.lastSelected = item.id
  }
}

状态管理方案

对于复杂应用,建议使用Vuex管理选中状态。

// store.js
export default new Vuex.Store({
  state: {
    selectedItems: []
  },
  mutations: {
    TOGGLE_ITEM(state, itemId) {
      const index = state.selectedItems.indexOf(itemId)
      if (index === -1) {
        state.selectedItems.push(itemId)
      } else {
        state.selectedItems.splice(index, 1)
      }
    },
    CLEAR_SELECTION(state) {
      state.selectedItems = []
    }
  }
})

样式优化技巧

添加CSS改善用户体验,如悬停效果和选中状态。

vue实现表格复选

tr:hover {
  background-color: #f5f5f5;
}
tr.selected {
  background-color: #e3f2fd;
}
input[type="checkbox"] {
  cursor: pointer;
}

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

相关文章

国外vue实现表格控件

国外vue实现表格控件

以下是国外流行的 Vue 表格控件实现方案及特点,供参考: vue-good-table 基于 Vue 2/3 的轻量级表格组件,支持排序、分页、筛选等功能。 安装方式: npm instal…

vue实现简单表格

vue实现简单表格

使用 Vue 实现简单表格 Vue 提供了多种方式实现表格功能,以下是基于 Vue 3 的两种常见方法: 基础表格实现 通过 v-for 指令循环渲染表格数据: <template&…

vue实现导出表格

vue实现导出表格

Vue 实现导出表格的方法 使用 xlsx 库导出 Excel 安装 xlsx 库 npm install xlsx 在 Vue 组件中引入 xlsx import * as XLSX from…

使用vue实现表格

使用vue实现表格

使用 Vue 实现表格 Vue.js 提供了灵活的方式来实现表格功能,可以通过组合 v-for 指令和动态数据绑定快速构建表格。以下是几种常见的实现方法: 基础表格实现 通过 v-for 遍历数组数…

vue实现复选功能

vue实现复选功能

Vue 实现复选功能 基础实现 使用 v-model 绑定到数组实现多选功能。当复选框被选中时,其 value 会被添加到数组中;取消选中时,会从数组中移除。 <template>…

vue实现分组表格

vue实现分组表格

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