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('刷新完成')
  }
相关推荐
共享家95273 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Halo_tjn4 小时前
基于封装的专项 知识点
java·前端·python·算法
有来技术5 小时前
Spring Boot 4 + Vue3 企业级多租户 SaaS:从共享 Schema 架构到商业化套餐设计
java·vue.js·spring boot·后端
东东5166 小时前
学院个人信息管理系统 (springboot+vue)
vue.js·spring boot·后端·个人开发·毕设
m0_748229997 小时前
Vue2 vs Vue3:核心差异全解析
前端·javascript·vue.js
C澒7 小时前
前端监控系统的最佳实践
前端·安全·运维开发
xiaoxue..7 小时前
React 手写实现的 KeepAlive 组件
前端·javascript·react.js·面试
hhy_smile7 小时前
Class in Python
java·前端·python
小邓吖8 小时前
自己做了一个工具网站
前端·分布式·后端·中间件·架构·golang
南风知我意9578 小时前
【前端面试2】基础面试(杂项)
前端·面试·职场和发展