Vue3之ElementPlus中Table选中数据的获取与清空方法

1. 点击按钮获取与清空选中表格的数据

1. 用到ElementPlus中Table的两个方法

这里需要用到Element-plus中Table 方法的两个方法

getSelectionRows 返回当前选中的行
clearSelection 用于多选表格,清空用户的选择

2. 业务场景

业务场景:根据操作按钮获取表格选中的数据,关闭组件或点取消按钮时清空选中的表格数据

  1. 获取表格数据:调用getSelectionRows方法,如tableRef.value.getSelectionRows()

  2. 清空已选择的表格数据:调用clearSelection方法,如:tableRef.value.clearSelection()

3. 操作案例

  1. 定义表格信息
<template>
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="1500px" append-to-body>
        <el-table ref="tableRef" :data="entrustProjectList" @selection-change="handleProject">
                    <el-table-column type="selection" width="55" align="center" />
                    <el-table-column label="名称" align="center" prop="name" />
                    </el-table-column>
                  </el-table>
        <template #footer>
                <div class="dialog-footer">
                  <el-button type="primary" @click="getSelectedTableData">获取选中的表格数据</el-button>
                   <el-button @click="clearSelectedTableInfo">清空选中的表格数据</el-button>
                </div>
              </template>
    </el-dialog>
</templte>
  1. 完整案例
<template>
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="1500px" append-to-body>
        <el-table ref="tableRef" :data="entrustProjectList" @selection-change="handleProject">
                    <el-table-column type="selection" width="55" align="center" />
                    <el-table-column label="名称" align="center" prop="name" />
                    </el-table-column>
                  </el-table>
        <template #footer>
                <div class="dialog-footer">
                  <el-button type="primary" @click="getSelectedTableData">获取选中的表格数据</el-button>
                   <el-button @click="clearSelectedTableInfo">清空选中的表格数据</el-button>
                </div>
              </template>
    </el-dialog>
</templte>
<script setup lang="ts">
import {ref} from "vue";
//表格组件
const tableRef = ref()
/**
 * 获取表格数据按钮事件
 */
const getSelectedTableData = () => {
 //通过Element-Plus表格的getSelectionRows的方法,获取已选中的数据
 let tableData = tableRef.value.getSelectionRows();
  console.log("选中数据",tableData)
};
    
const clearSelectedTableInfo = () => {
  console.log("清空选中数据前==",tableRef.value.getSelectionRows())
  //清空数据,通过Element-Plus表格的clearSelection的方法,清空所有已选中的数据
  tableRef.value.clearSelection()
  console.log("清空选中数据后==",tableRef.value.getSelectionRows())
};    
</script>
GitHub 加速计划 / vu / vue
97
16
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:8 个月前 )
9e887079 [skip ci] 6 个月前
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 10 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐