当前位置:首页 > 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提供了现成的表格复选框组件:

vue表格实现复选框

<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>

全选/反选功能实现

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

vue表格实现复选框

<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等库实现:

<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实现表格拖动列宽

实现表格列宽拖动的核心思路 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖动逻辑。在表头列分隔线处添加可拖拽区域,拖动时动态修改列的宽度。 基础HTML结构…

vue表格实现复选框

vue表格实现复选框

vue表格实现复选框 在Vue中实现表格复选框功能,可以通过以下方法完成: 方法一:使用el-table(Element UI) 安装Element UI后,在表格列中直接添加type="se…

vue 实现表格单选

vue 实现表格单选

实现表格单选的方法 在Vue中实现表格单选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-model绑定选中项 通过v-model绑定一个变量来记录当前选中的行数据。当用户点击某一行时…

vue实现简单表格

vue实现简单表格

Vue 实现简单表格的方法 使用原生 HTML 表格 在 Vue 模板中可以直接使用 HTML 原生表格标签,通过 v-for 动态渲染数据: <template> <tabl…

使用vue实现表格

使用vue实现表格

使用 Vue 实现表格 在 Vue 中实现表格可以通过多种方式完成,以下是一个基于 Vue 3 的示例,展示如何动态渲染表格数据并支持基础功能(如排序、筛选)。 基础表格实现 通过 v-for 循环…

css表格表单制作

css表格表单制作

CSS表格制作 使用CSS创建表格主要涉及<table>标签及其子元素的样式控制。以下是一个基础示例: <table class="styled-table"> <…