当前位置:首页 > 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;
}

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

相关文章

css制作三行三列表格

css制作三行三列表格

使用HTML和CSS创建三行三列表格 通过HTML的<table>标签结合CSS样式可以轻松实现三行三列的表格结构。 <table class="three-by-three"&g…

vue实现表格

vue实现表格

Vue实现表格的方法 在Vue中实现表格可以通过多种方式完成,包括使用原生HTML表格、第三方组件库或自定义组件。以下是几种常见的方法: 使用原生HTML表格 在Vue模板中直接使用HTML的&l…

vue实现动态用户表格

vue实现动态用户表格

Vue 动态用户表格实现方法 基础表格结构 使用<table>或第三方组件库(如Element UI的el-table)构建基础框架。静态数据示例: <template>…

uniapp 表格导入

uniapp 表格导入

uniapp 表格导入的实现方法 使用 uni.chooseFile 选择文件 在 uniapp 中,可以通过 uni.chooseFile API 让用户选择本地文件。该 API 支持多平台,包括…

使用vue实现表格

使用vue实现表格

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

vue实现表格内容

vue实现表格内容

Vue实现表格内容的方法 使用原生HTML表格 在Vue模板中直接使用HTML的<table>标签构建表格结构,通过v-for指令动态渲染数据。 <template> &…