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官网

相关推荐
excel10 小时前
Vue 编译器兼容性系统源码详解
前端
excel10 小时前
Vue 编译器源码解析:noopDirectiveTransform 的作用与设计哲学
前端
uhakadotcom10 小时前
基于 TOON + Next.js 来大幅节省 token 并运行大模型
前端·面试·github
excel10 小时前
🧠 Vue 编译器的表达式处理:transformExpression 通俗讲解
前端
excel10 小时前
一份 TypeScript 声明文件的全景解析:从全局常量到模块扩展
前端
excel10 小时前
Vue 编译器中的过滤器转换机制(transformFilter)详解
前端
Glommer10 小时前
验证码滑动轨迹浅谈
javascript·逆向
excel10 小时前
Vue 编译器中的静态节点缓存机制:cacheStatic() 深度解析
前端
用户114818678948410 小时前
深入理解 V8 引擎--理解版
前端
inx17710 小时前
深入理解 CSS 盒模型:box-sizing 的正确使用姿势
前端·css