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

相关推荐
wuhen_n17 分钟前
响应式探秘:ref vs reactive,我该选谁?
前端·javascript·vue.js
wuhen_n17 分钟前
setup 的艺术:如何组织我们的组合式函数?
前端·javascript·vue.js
三翼鸟数字化技术团队31 分钟前
前端架构演进与模块化设计实践
前端·架构
Moment37 分钟前
Cursor 的 5 种指令方法比较,你最喜欢哪一种?
前端·后端·github
IT_陈寒41 分钟前
Vite快得离谱?揭秘它比Webpack快10倍的5个核心原理
前端·人工智能·后端
明月_清风1 小时前
性能级目录同步:IntersectionObserver 实战
前端·javascript
明月_清风1 小时前
告别暴力轮询:深度解锁浏览器“观察者家族”
前端·javascript
摸鱼的春哥1 小时前
Agent教程17:LangChain的持久化和人工干预
前端·javascript·后端
程序员爱钓鱼3 小时前
Go操作Excel实战详解:github.com/xuri/excelize/v2
前端·后端·go
子兮曰11 小时前
async/await高级模式:async迭代器、错误边界与并发控制
前端·javascript·github