当前位置:首页 > VUE

vue表格实现复选框

2026-01-12 07:31:46VUE

Vue表格实现复选框的方法

基础实现(使用v-model绑定数组)

在Vue中可以通过v-model绑定数组来实现复选框功能。以下是一个基础示例:

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

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ],
      selectedItems: []
    }
  }
}
</script>

使用Element UI等UI库

主流UI库如Element UI提供了现成的表格复选框组件:

<template>
  <el-table
    :data="tableData"
    @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>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ],
      multipleSelection: []
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>

全选/反选功能实现

添加表头复选框控制全选状态:

<template>
  <table>
    <thead>
      <tr>
        <th><input type="checkbox" v-model="selectAll" @change="toggleSelectAll"></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" @change="updateSelectAll"></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' }
      ],
      selectedItems: [],
      selectAll: false
    }
  },
  methods: {
    toggleSelectAll() {
      this.selectedItems = this.selectAll 
        ? this.tableData.map(item => item.id) 
        : []
    },
    updateSelectAll() {
      this.selectAll = this.selectedItems.length === this.tableData.length
    }
  }
}
</script>

自定义复选框样式

通过CSS自定义复选框外观:

.custom-checkbox {
  position: relative;
  display: inline-block;
  width: 18px;
  height: 18px;
}

.custom-checkbox input {
  opacity: 0;
  position: absolute;
}

.custom-checkbox .checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 18px;
  width: 18px;
  background-color: #eee;
  border-radius: 3px;
}

.custom-checkbox input:checked ~ .checkmark {
  background-color: #2196F3;
}

性能优化建议

对于大型数据表格,考虑使用虚拟滚动技术避免渲染过多DOM节点。可以结合vue-virtual-scroller等库实现:

vue表格实现复选框

<template>
  <RecycleScroller
    class="table"
    :items="tableData"
    :item-size="50">
    <template v-slot="{ item }">
      <tr>
        <td><input type="checkbox" v-model="selectedItems" :value="item.id"></td>
        <td>{{ item.name }}</td>
      </tr>
    </template>
  </RecycleScroller>
</template>

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

相关文章

如何实现vue表格联动

如何实现vue表格联动

实现 Vue 表格联动的方法 数据绑定与响应式更新 通过 Vue 的响应式特性,将多个表格的数据绑定到同一个数据源或计算属性。当一个表格的数据发生变化时,另一个表格会自动更新。例如: data()…

vue element实现表格

vue element实现表格

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

vue实现表格筛选

vue实现表格筛选

实现表格筛选的基本思路 在Vue中实现表格筛选通常涉及以下几个核心步骤:数据绑定、筛选逻辑处理、动态渲染。以下是具体实现方法。 基础数据绑定与表格渲染 定义表格数据和表头结构,使用v-for循环渲染…

vue实现表格报表

vue实现表格报表

Vue 实现表格报表的方法 使用 Element UI 的表格组件 Element UI 提供了功能丰富的表格组件,适合快速实现报表功能。安装 Element UI 后,可以直接使用 el-table…

vue实现表格搜索

vue实现表格搜索

实现表格搜索功能 在Vue中实现表格搜索功能,可以通过以下步骤完成: 数据绑定与搜索输入框 创建一个输入框用于接收用户输入的搜索关键词,使用v-model实现双向绑定。 <template…

vue 实现简单表格

vue 实现简单表格

以下是在 Vue 中实现简单表格的几种方法: 使用原生表格标签 通过 Vue 的数据绑定功能渲染表格数据: <template> <table> <the…