• 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
  • 相关阅读:
    MOSFET, MOS管, 开关管笔记
    clazzToJsonDefault java 实体 to json 字符串
    计算机网络与互联网
    原理Redis-Dict字典
    gitlab Blocking and unblocking users
    GAN入门|第四篇:生成手势图像|可控制生成
    笔记(上):mysql-DuplicateUpdate和java的threadpool的“死锁“
    sheng的学习笔记-【中英】【吴恩达课后测验】Course 1 - 神经网络和深度学习 - 第四周测验
    RT-Thread 中断管理学习(二)
    Swing基本组件的用法(一)
  • 原文地址:https://blog.csdn.net/yuanjinshenglife/article/details/136114140