当前位置:首页 > VUE

vue实现树型组件

2026-02-22 04:18:11VUE

vue实现树型组件

数据结构和递归组件

树型组件通常需要递归渲染,数据格式一般为嵌套结构。常见的数据结构如下:

treeData: [
  {
    id: 1,
    label: '节点1',
    children: [
      {
        id: 2,
        label: '节点1.1',
        children: []
      }
    ]
  }
]

递归组件通过组件自身调用自身实现:

<template>
  <ul>
    <li v-for="item in data" :key="item.id">
      {{ item.label }}
      <tree-node v-if="item.children" :data="item.children"/>
    </li>
  </ul>
</template>

<script>
export default {
  name: 'TreeNode',
  props: ['data']
}
</script>

展开/折叠功能

通过添加isOpen状态控制子节点显示:

vue实现树型组件

<template>
  <ul>
    <li v-for="item in data" :key="item.id">
      <span @click="toggle(item)">{{ item.isOpen ? '-' : '+' }}</span>
      {{ item.label }}
      <tree-node 
        v-if="item.children && item.isOpen" 
        :data="item.children"
      />
    </li>
  </ul>
</template>

<script>
export default {
  methods: {
    toggle(item) {
      this.$set(item, 'isOpen', !item.isOpen)
    }
  }
}
</script>

复选框功能

添加全选/半选状态需要计算选中状态:

<template>
  <ul>
    <li v-for="item in data" :key="item.id">
      <input 
        type="checkbox" 
        v-model="item.checked"
        @change="handleCheckChange(item)"
      >
      {{ item.label }}
      <tree-node 
        v-if="item.children" 
        :data="item.children"
      />
    </li>
  </ul>
</template>

<script>
export default {
  methods: {
    handleCheckChange(item) {
      // 向下同步选中状态
      if (item.children) {
        item.children.forEach(child => {
          child.checked = item.checked
        })
      }
      // 向上同步选中状态
      this.updateParentChecked(item)
    },
    updateParentChecked(item) {
      // 实现父节点状态更新逻辑
    }
  }
}
</script>

动态加载

实现懒加载需要添加加载方法:

vue实现树型组件

<template>
  <ul>
    <li v-for="item in data" :key="item.id">
      <span @click="loadChildren(item)">加载</span>
      {{ item.label }}
      <tree-node 
        v-if="item.children" 
        :data="item.children"
      />
    </li>
  </ul>
</template>

<script>
export default {
  methods: {
    async loadChildren(item) {
      if (!item.children) {
        const res = await api.getChildren(item.id)
        this.$set(item, 'children', res.data)
      }
    }
  }
}
</script>

样式优化

添加缩进和图标增强视觉效果:

.tree-node {
  padding-left: 20px;
  position: relative;
}
.tree-node:before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 16px;
  height: 100%;
  border-left: 1px solid #ddd;
}

性能优化

对于大型树结构,建议使用虚拟滚动:

<template>
  <virtual-list :size="40" :remain="20">
    <tree-node v-for="item in visibleData" :data="item"/>
  </virtual-list>
</template>

标签: 树型组件
分享给朋友:

相关文章

uniapp组件写法

uniapp组件写法

Uniapp 组件的基本写法 Uniapp 的组件写法与 Vue 类似,支持单文件组件(.vue 文件)。一个基本的组件通常包含三个部分:<template>、<script>…

vue实现广告组件

vue实现广告组件

vue实现广告组件的方法 使用动态组件实现 在Vue中可以通过动态组件的方式加载广告组件,根据不同的条件展示不同的广告内容。动态组件使用<component :is="currentCompon…

uniapp 分页组件

uniapp 分页组件

uniapp 分页组件实现方法 在uniapp中实现分页功能,可以通过自定义组件或使用第三方组件库完成。以下是几种常见实现方式: 自定义分页组件 创建一个名为uni-pagination的组件,模板…

vue 实现table组件

vue 实现table组件

Vue 实现 Table 组件 基础表格结构 使用 Vue 的模板语法构建表格的基本框架,通过 v-for 动态渲染数据。 <template> <table>…

vue实现拖拽组件

vue实现拖拽组件

Vue 实现拖拽组件的方法 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue基础组件实现

vue基础组件实现

Vue 基础组件实现 组件定义 使用 Vue.component 全局注册或局部注册组件。全局注册适合通用组件,局部注册适合特定页面使用的组件。 // 全局注册 Vue.component('my-…