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

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>
  • 27
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值