当前位置:首页 > 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>

分页全选功能

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

<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中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容切…

vue实现收起展开面板

vue实现收起展开面板

实现收起展开面板的方法 在Vue中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起:…

vue实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <templat…

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 297…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-…