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

相关推荐
kite01215 小时前
浏览器工作原理06 [#]渲染流程(下):HTML、CSS和JavaScript是如何变成页面的
javascript·css·html
крон5 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
coding随想7 小时前
JavaScript ES6 解构:优雅提取数据的艺术
前端·javascript·es6
年老体衰按不动键盘7 小时前
快速部署和启动Vue3项目
java·javascript·vue
小小小小宇7 小时前
一个小小的柯里化函数
前端
灵感__idea7 小时前
JavaScript高级程序设计(第5版):无处不在的集合
前端·javascript·程序员
小小小小宇7 小时前
前端双Token机制无感刷新
前端
小小小小宇7 小时前
重提React闭包陷阱
前端
小小小小宇7 小时前
前端XSS和CSRF以及CSP
前端
UFIT7 小时前
NoSQL之redis哨兵
java·前端·算法