当前位置:首页 > VUE

vue el实现全选

2026-01-19 05:41:23VUE

Vue Element UI 实现全选功能

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

基础全选功能

在表格的列中添加一个全选复选框,并通过 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>

分页全选功能

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

vue el实现全选

<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
分享给朋友:

相关文章

vue实现文档

vue实现文档

Vue 实现文档的方法 在 Vue 中实现文档功能可以通过多种方式,包括使用富文本编辑器、Markdown 解析或直接渲染 HTML。以下是几种常见的实现方法。 使用富文本编辑器 富文本编辑器如 T…

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HTM…

vue实现博客

vue实现博客

Vue 实现博客的基本步骤 使用 Vue.js 实现一个博客系统可以分为前端和后端两部分。以下是基于 Vue 的前端实现方案,后端可以选择 Node.js、Django 或其他框架。 项目初始化 使…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue实现dag

vue实现dag

Vue实现DAG(有向无环图) 在Vue中实现DAG(Directed Acyclic Graph,有向无环图)通常涉及数据结构的建模、可视化渲染以及交互逻辑处理。以下是关键实现步骤和示例代码: 数…