当前位置:首页 > VUE

vue el实现全选

2026-01-19 05:41:23VUE

Vue Element UI 实现全选功能

使用 Vue 和 Element UI 实现表格全选功能,可以通过 el-tableel-checkbox 组件结合实现。以下是具体实现方法:

vue el实现全选

基础全选功能

在表格的列中添加一个全选复选框,并通过 v-model 绑定选中状态。

<template>
  <el-table
    :data="tableData"
    ref="multipleTable"
    @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-column prop="age" label="Age"></el-table-column>
  </el-table>

  <el-checkbox v-model="selectAll" @change="toggleSelectAll">全选</el-checkbox>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: 'John', age: 20 },
        { name: 'Jane', age: 22 },
        { name: 'Tom', age: 25 }
      ],
      selectAll: false,
      selectedItems: []
    }
  },
  methods: {
    toggleSelectAll(val) {
      this.$refs.multipleTable.toggleAllSelection();
    },
    handleSelectionChange(val) {
      this.selectedItems = val;
      this.selectAll = val.length === this.tableData.length;
    }
  }
}
</script>

分页全选功能

如果表格有分页,需要处理跨页全选逻辑。

<template>
  <el-table
    :data="currentPageData"
    ref="multipleTable"
    @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>

  <el-checkbox v-model="selectAll" @change="toggleSelectAll">全选</el-checkbox>
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="total"
  ></el-pagination>
</template>

<script>
export default {
  data() {
    return {
      tableData: [...], // 所有数据
      currentPage: 1,
      pageSize: 10,
      selectAll: false,
      selectedItems: [],
      allSelected: false
    }
  },
  computed: {
    currentPageData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.tableData.slice(start, end);
    },
    total() {
      return this.tableData.length;
    }
  },
  methods: {
    toggleSelectAll(val) {
      if (val) {
        this.currentPageData.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row, true);
        });
        this.allSelected = true;
      } else {
        this.$refs.multipleTable.clearSelection();
        this.allSelected = false;
      }
    },
    handleSelectionChange(val) {
      this.selectedItems = val;
      this.selectAll = val.length === this.currentPageData.length;
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      if (this.allSelected) {
        this.$nextTick(() => {
          this.currentPageData.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row, true);
          });
        });
      }
    }
  }
}
</script>

注意事项

  • 使用 toggleAllSelection() 方法可以切换所有行的选中状态
  • 分页情况下需要额外处理跨页选择逻辑
  • selection-change 事件会在选择变化时触发
  • 使用 clearSelection() 可以清除所有选择

以上方法可以灵活应用于各种 Vue + Element UI 的表格全选场景。

vue el实现全选

标签: 全选vue
分享给朋友:

相关文章

vue 实现多选

vue 实现多选

Vue 实现多选的方法 在 Vue 中实现多选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-model 绑定数组 Vue 的 v-model 指令可以与复选框结合使用,绑定到一个数…

vue如何实现注册

vue如何实现注册

实现用户注册功能 在Vue中实现注册功能通常需要结合前端表单和后端API交互。以下是完整的实现方案: 创建注册表单组件 注册表单需要包含用户名、邮箱、密码等基本字段,并进行表单验证: <…

vue播放倍速怎么实现

vue播放倍速怎么实现

实现Vue播放倍速的方法 在Vue中实现播放倍速功能通常涉及HTML5的<video>元素或第三方播放器库。以下是几种常见实现方式: 使用原生HTML5 video元素 通过ref获取…

vue设计与实现 书

vue设计与实现 书

vue设计与实现 书 《Vue.js设计与实现》是一本深入探讨Vue.js框架内部原理和设计思想的书籍,适合希望深入理解Vue.js的开发者。以下是关于这本书的详细信息: 书籍内容 《Vue…

vue实现视频会议

vue实现视频会议

使用 Vue 实现视频会议 技术选型 Vue.js 作为前端框架,结合 WebRTC 技术实现实时音视频通信。常用的库包括: peerjs:简化 WebRTC 的点对点连接。 socket.io:…

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name…