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('刷新完成')
  }
相关推荐
于慨15 小时前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
石小石Orz15 小时前
油猴脚本实现生产环境加载本地qiankun子应用
前端·架构
从前慢丶15 小时前
前端交互规范(Web 端)
前端
像我这样帅的人丶你还15 小时前
别再让JS耽误你进步了。
css·vue.js
@yanyu66615 小时前
07-引入element布局及spring boot完善后端
javascript·vue.js·spring boot
CHU72903515 小时前
便捷约玩,沉浸推理:线上剧本杀APP功能版块设计详解
前端·小程序
GISer_Jing15 小时前
Page-agent MCP结构
前端·人工智能
王霸天15 小时前
💥别再抄网上的Scale缩放代码了!50行源码教你写一个永不翻车的大屏适配
前端·vue.js·数据可视化
小领航15 小时前
用 Three.js + Vue 3 打造炫酷的 3D 行政地图可视化组件
前端·github
@大迁世界15 小时前
2026年React大洗牌:React Hooks 将迎来重大升级
前端·javascript·react.js·前端框架·ecmascript