当前位置:首页 > VUE

vue表格实现复选框

2026-03-06 20:28:30VUE

实现复选框的基本方法

在Vue中实现表格复选框功能,可以通过v-model绑定数据,结合v-for渲染表格行。以下是一个基础示例:

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

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

使用第三方组件库

若使用Element UI等组件库,可直接调用现成的表格复选框功能:

<template>
  <el-table
    :data="items"
    @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 {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ],
      selectedItems: []
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.selectedItems = val;
    }
  }
}
</script>

自定义复选框样式

通过CSS自定义复选框样式,提升视觉效果:

.custom-checkbox {
  appearance: none;
  width: 16px;
  height: 16px;
  border: 1px solid #ccc;
  border-radius: 3px;
  cursor: pointer;
  position: relative;
}

.custom-checkbox:checked {
  background-color: #409EFF;
  border-color: #409EFF;
}

.custom-checkbox:checked::after {
  content: "✓";
  color: white;
  position: absolute;
  left: 3px;
  top: -1px;
}

性能优化建议

对于大数据量表格,建议采用虚拟滚动或分页加载。可通过计算属性优化选中状态判断:

computed: {
  isAllSelected() {
    return this.selectedItems.length === this.items.length;
  }
}

完整示例代码

以下是一个包含全选/反选功能的完整实现:

vue表格实现复选框

<template>
  <div>
    <button @click="toggleAll">{{ isAllSelected ? 'Unselect All' : 'Select All' }}</button>
    <table>
      <tbody>
        <tr v-for="item in items" :key="item.id">
          <td>
            <input 
              type="checkbox" 
              :checked="isSelected(item.id)"
              @change="toggleSelect(item.id)"
            >
          </td>
          <td>{{ item.name }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [/* your data */],
      selectedItems: new Set()
    }
  },
  computed: {
    isAllSelected() {
      return this.selectedItems.size === this.items.length;
    }
  },
  methods: {
    toggleSelect(id) {
      this.selectedItems.has(id) 
        ? this.selectedItems.delete(id) 
        : this.selectedItems.add(id);
    },
    toggleAll() {
      if (this.isAllSelected) {
        this.selectedItems.clear();
      } else {
        this.items.forEach(item => this.selectedItems.add(item.id));
      }
    },
    isSelected(id) {
      return this.selectedItems.has(id);
    }
  }
}
</script>

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

相关文章

vue实现表格复选

vue实现表格复选

Vue实现表格复选的方法 基础实现 在Vue中实现表格复选功能可以通过v-model绑定复选框的状态,结合v-for循环渲染表格数据。以下是一个基础示例: <template> &l…

vue实现导出表格

vue实现导出表格

Vue 中实现表格导出 使用 xlsx 库导出 Excel 安装 xlsx 库: npm install xlsx --save 在 Vue 组件中引入并使用: import XLSX from…

用css制作表格

用css制作表格

使用CSS制作表格 CSS可以用来样式化HTML表格,使其更具视觉吸引力和功能性。以下是几种常见的方法: 基础表格样式 通过CSS可以调整表格的边框、间距和颜色: <table>…

uniapp 表格导入

uniapp 表格导入

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

css怎么制作表格

css怎么制作表格

使用HTML和CSS制作表格 在HTML中,表格由<table>标签定义,结合CSS可以灵活控制样式。 基本HTML结构: <table> <tr>…

vue实现表格内容

vue实现表格内容

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