vue 树组件 vxe-tree 懒加载同时支持拖拽排序与拖拽到子节点

树数据量大时,一次性加载全部节点既慢又占内存。vxe-tree 支持懒加载:只加载根节点,展开时再异步请求子节点。在此基础上,还可以同时开启拖拽排序与拖拽到子节点;当拖到尚未加载的节点时,通过 isAutoExpandToChild 自动展开并触发加载。

vxe-tree 如何启用懒加载?

在树配置中设置 lazy: true,并通过 hasChild(或 hasChildField 指定字段名)标记节点是否有子节点;提供 loadMethod({ node }) 返回 Promise,展开节点时自动调用并加载子节点。

  • lazy: true 必须开启,否则展开时不会调用 loadMethod。
  • 节点数据需带 hasChild 字段:true 表示有子节点,展开时触发加载;false 表示叶子节点,不显示展开图标。
  • loadMethod 必须返回 Promise,解析结果为子节点数组;异步加载期间节点显示加载状态。
  • transform: true 时,数据以平铺数组提供,通过 id / parentId 建立层级;懒加载场景下根节点 parentId 可为空。

初始只显示根节点;点击展开图标后,约 500ms 后子节点出现;子节点若 hasChild: true,可继续展开加载下一层。

懒加载下勾选父节点,子节点会自动选中吗?

会。开启 showCheckbox 后,勾选一个尚未加载子节点的父节点,当该父节点展开并异步加载出子节点时,这些子节点会被自动选中,与父节点的勾选状态保持一致

  • 这是 vxe-tree 懒加载与勾选联动的内置行为,无需额外配置。
  • 适合“全选某分类下所有子项”的场景:用户只需勾选父节点,无需等子节点加载完再逐个勾选。
  • 若业务不希望子节点自动继承勾选状态,需在子节点加载后手动处理选中状态。

vxe-tree 如何在懒加载的同时支持拖拽排序与拖拽到子节点?

在树配置中同时设置 drag: true 与 dragConfig,按需开启 isCrossDrag、isToChildDrag、isAutoExpandToChild 等能力。lazy 与 drag 可以共存。

关键配置:

配置项 作用
drag: true 开启拖拽总开关
dragConfig.isCrossDrag 允许跨层级拖拽(不同父节点之间)
dragConfig.isSelfToChildDrag 允许拖拽自身到自己的子节点
dragConfig.isToChildDrag 允许拖拽到子节点位置
dragConfig.isAutoExpandToChild 拖拽到子级时自动展开父级;若为懒加载节点,自动触发加载

默认情况下,懒加载节点必须先展开过、子节点已加载,才允许拖入其子级。开启 isAutoExpandToChild 后,拖拽到未展开的节点上时,该节点会自动展开并调用 loadMethod 加载子节点,拖拽落点随之生效。

懒加载使用

当节点被勾选是,异步加载后的子节点也会被自动选中

tree_lazy_load_method

<template>
  <div>
    <vxe-tree v-bind="treeOptions"></vxe-tree>
  </div>
</template>

<script setup>
import { reactive } from 'vue'

// 模拟后端接口
const getNodeListApi = (node) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve([
        { title: `${node.title}-1`, id: `${node.id}1`, hasChild: Math.random() * 10 < 6 },
        { title: `${node.title}-2`, id: `${node.id}2`, hasChild: Math.random() * 10 < 6 }
      ])
    }, 500)
  })
}

const treeOptions = reactive({
  transform: true,
  lazy: true,
  showCheckbox: true,
  nodeConfig: {
    isHover: true
  },
  loadMethod({ node }) {
    return getNodeListApi(node)
  },
  data: [
    { title: '节点2', id: '2', hasChild: true },
    { title: '节点3', id: '3', hasChild: true },
    { title: '节点4', id: '4', hasChild: true },
    { title: '节点5', id: '5', hasChild: false }
  ]
})
</script>

同时使用拖拽

通过 drag-config.isAutoExpandToChild 设置在拖拽到子级时自动展开父级,如果是懒加载行则自动触发加载

tree_drag_lazy2

<template>
  <div>
    <vxe-tree v-bind="treeOptions"></vxe-tree>
  </div>
</template>

<script setup>
import { reactive } from 'vue'

// 模拟后端接口
const getNodeListApi = (node) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve([
        { title: `${node.title}-1`, id: `${node.id}1`, hasChild: Math.random() * 10 < 6 },
        { title: `${node.title}-2`, id: `${node.id}2`, hasChild: Math.random() * 10 < 6 }
      ])
    }, 500)
  })
}

const treeOptions = reactive({
  transform: true,
  lazy: true,
  drag: true,
  dragConfig: {
    isCrossDrag: true,
    isSelfToChildDrag: true,
    isToChildDrag: true,
    isAutoExpandToChild: true
  },
  nodeConfig: {
    isHover: true
  },
  loadMethod({ node }) {
    return getNodeListApi(node)
  },
  data: [
    { title: '节点2', id: '2', hasChild: true },
    { title: '节点3', id: '3', hasChild: true },
    { title: '节点4', id: '4', hasChild: true },
    { title: '节点5', id: '5', hasChild: false }
  ]
})
</script>

关键配置参数

配置项 类型 默认值 说明
lazy Boolean false 是否启用懒加载
loadMethod Function — 展开节点时调用,参数 { node },返回 Promise
hasChild / hasChildField String 'hasChild' 节点数据中标记是否有子节点的字段名
transform Boolean false 平铺数据转树形结构
showCheckbox Boolean false 是否显示复选框;懒加载下勾选父节点,子节点加载后自动选中
drag Boolean false 拖拽总开关
dragConfig.isCrossDrag Boolean false 是否允许跨层级拖拽
dragConfig.isSelfToChildDrag Boolean false 是否允许拖拽自身到自己的子节点
dragConfig.isToChildDrag Boolean false 是否允许拖拽到子节点
dragConfig.isAutoExpandToChild Boolean false 拖拽到子级时自动展开;懒加载节点自动触发加载
nodeConfig.isHover Boolean false 鼠标悬停时高亮节点

https://vxeui.com

posted @ 2026-12-01 09:51  独行者r2  阅读(0)  评论(0)    收藏  举报