Vue3 el-table 默认选中 传入的数组

一、效果:

二、官网是VUE2 现更改为Vue3写法

javascript 复制代码
<template>
  <el-table
      :data="tableData"
      border stripe
      row-key="id"
      ref="tableRef"
      :cell-style="{ 'text-align': 'center' }"
      :header-cell-style="{
            background: '#b7babd',
            color: '#1e1f22',
            height: '35px',
            'text-align': 'center'
          }"
      v-loading="tableLoading"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40px" :reserve-selection="true"/>
      <el-table-column fixed prop="id" label="序号" width="60px" type="index"/>
      <el-table-column label="编码" align="center" width="140px" prop="code"/>
      <el-table-column label="名称" align="center" width="120px" prop="name" />
    </el-table>
</template>
javascript 复制代码
<script setup lang="ts" name="rulesTable">
    const tableRef = ref()
    const isSelectIds = ref([]) // 选中的id

    const handleSelectionChange = (selection: any[]) => {
      isSelectIds.value = []
      const ids = selection.map(item => item.id);
      const names = selection.map(item => item.excptName);
      isSelectIds.value = ids;
    }

    const open = async (selectIds: Array<any>, type: string) => {
          dialogVisible.value = true
          isSelectIds.value = selectIds
          isSelectNames.value = ""
          tableLoading.value = true
          try {
            await EnergyBaseMeterInfoApi.getRulesTableList(type).then((res)=>{
              tableData.value = res

                 // todo 假设默认选中的是
                  isSelectIds.value = [12323123,232323234]
                 // todo 假设默认选中的是

              // 循环需要选中的数组
              isSelectIds.value.forEach(id => {
              // 在数据源中判断是否存在
              const row = tableData.value.find(r => String(r.id) === id);
                if (row) {
                  tableRef.value.toggleRowSelection(row, true);
                }
             });
            })
          } finally {
            tableLoading.value = false
          }
        }

    defineExpose({ open })
</script>

element官网

相关推荐
IT_陈寒3 分钟前
JavaScript性能优化:5个V8引擎隐藏技巧让你的代码提速50%
前端·人工智能·后端
菠菜盼娣11 分钟前
第三方插件 unplugin-icons
前端
[seven]15 分钟前
React Router TypeScript 路由详解:类型安全的路由配置与参数处理
安全·react.js·typescript
敲代码的彭于晏16 分钟前
在迁移中学习 React 18:一份来自 React 17 的升级问题清单
前端·react.js
顾安r22 分钟前
12.17 脚本工具 自动化全局跳转
linux·前端·css·golang·html
踢球的打工仔23 分钟前
jquery的基本使用(2)
前端·javascript·jquery
阿蒙Amon28 分钟前
JavaScript学习笔记:16.模块
javascript·笔记·学习
DEMO派38 分钟前
前端javascript如何实现阅读位置记忆【可运行源码】
前端
苏打水com42 分钟前
第十七篇:Day49-51 前端工程化进阶——从“手动”到“自动化”(对标职场“提效降本”需求)
前端·javascript·css·vue.js·html
文心快码BaiduComate43 分钟前
Comate强力赋能:「趣绘像素岛」从体验泥潭到高性能可用的蜕变之路
前端·后端·程序员