当前位置:首页 > 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 表格实现方法 基础表格实现 使用 el-table 组件(Element UI)实现基础表格: <template> <el-table :data="tableDat…

vue实现表格多行修改

vue实现表格多行修改

实现表格多行修改的方法 数据绑定与表格渲染 使用v-model绑定表格数据到Vue实例的data属性。通过v-for循环渲染表格行,并为每行数据添加编辑状态标识。 <template>…

国外vue实现表格控件

国外vue实现表格控件

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

css 制作表格

css 制作表格

CSS 制作表格的方法 使用 CSS 制作表格可以通过多种方式实现,包括原生 HTML 表格样式、Flexbox 布局或 Grid 布局。以下是几种常见的方法: 原生 HTML 表格样式 HTML…

css制作表格

css制作表格

CSS 制作表格的方法 使用 CSS 可以灵活地创建和样式化表格,以下是几种常见的方法: 使用 HTML 表格标签结合 CSS 样式 通过 HTML 的 <table>、<tr&g…

vue实现简单表格

vue实现简单表格

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