Element-Plus el-table lazy 自动更新子列表

Element-Plus el-table组件 设置为lazy 加载子列表时,如果子列表处于打开状态,这时打开弹窗新增一条子列表记录,子列表不会刷新

这篇文章利用映射resolve回调函数,实现更新某一个父行下的子列表

复制代码
<el-table row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange" :load="load" lazy
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column type="selection" width="55" />
      <el-table-column label="页面或接口名称" align="center" prop="name" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="父节点ID" align="center" prop="parentId" />
      <el-table-column label="菜单类型" align="center" prop="menuType">
        <template #default="scope">
          <el-tag v-if="scope.row.menuType == 0" type="primary">页面</el-tag>
          <el-tag v-else-if="scope.row.menuType == 1" type="primary">页面下的接口</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="客户端类型" align="center" prop="clientType">
        <template #default="scope">
          <el-tag v-if="scope.row.clientType == 0" type="primary">app</el-tag>
          <el-tag v-else-if="scope.row.clientType == 1" type="primary">h5</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="页面或接口路径" align="center" prop="path" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="当前记录层级" align="center" prop="level" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('create',null, scope.row.id)"
            v-hasPermi="['jszj:mobile-menu:update']" v-if="scope.row.level == 1">
            添加
          </el-button>

          <el-button link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['jszj:mobile-menu:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['jszj:mobile-menu:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>


  const lastTimeParentIdAdd = ref() 


 /** 查询列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await MobileMenuApi.getMobileMenuPage(queryParams)
      list.value = data.list
      total.value = data.total
    } finally {
      loading.value = false
      if (lastTimeParentIdAdd.value != null) {
        refreshChildren(lastTimeParentIdAdd.value);
      }
    }
  }

  /** 添加/修改操作 */
  const formRef = ref()
  const openForm = (type: string, id?: number, parentId?: number) => {
    formRef.value.open(type, id, parentId)

    lastTimeParentIdAdd.value = parentId;
  }

const pNodeResolveMap = reactive({})

  const load = async (
    row,
    treeNode: unknown,
    resolve: (date) => void
  ) => {
    const data = await MobileMenuApi.getByParentId(row.id)
    resolve(data);

    pNodeResolveMap[row.id] = resolve;
  }

  const refreshChildren = async (parentId) => {
    console.log(parentId);

    const resolve = pNodeResolveMap[parentId];
    if (resolve == null) {
      return;
    }

    const data = await MobileMenuApi.getByParentId(parentId)

    resolve(data);

    console.log('刷新完成')
  }
相关推荐
天渺工作室8 小时前
实现一个adblock/adblock plus等浏览器广告拦截器检测插件
前端·javascript
阳光是sunny8 小时前
Vue 项目怎么做用户行为全链路监控?轻量插件方案详解
前端·面试·架构
ZhengEnCi8 小时前
Q04-Vite禁用CSS代码分割-解决生产环境样式加载顺序混乱问题
前端·vue.js·vite
九酒9 小时前
AI Agent 开发踩坑记:口播功能非得用 APP 原生实现吗?
前端·人工智能·agent
Jackson__9 小时前
做了一段时间的AI coding后,我终于搞清了 CLI 和 MCP 的区别
前端·agent·ai编程
IT_陈寒12 小时前
JavaScript项目实战经验分享
前端·人工智能·后端
用户479492835691513 小时前
6w star,GitHub 趋势第一的 Ponytail,这个agent插件到底在火什么
前端·后端
薛定喵的谔14 小时前
我开源了一个精致的 Next.js 博客模板:Skyplume
前端·前端框架·next.js
张龙68715 小时前
构建生产级 AI Agent:工具调用与记忆架构实战指南
前端
kyriewen16 小时前
2026 年了,还在用 Node.js?Bun 迁移实战:20 分钟搞定,附踩坑记录
前端·javascript·node.js