前端Vue uni-app App/小程序/H5 通用tree树形结构图

本文介绍了如何使用Vueuni-app和ECharts组件来创建一个通用的树形结构图,该组件适用于App、小程序和H5。通过组件化开发,可以提高开发效率,降低维护成本。示例展示了如何配置和使用这个tree树形结构图组件,以展示不同规模企业的行业分类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

随着技术的发展,开发的复杂度也越来越高,传统开发方式将一个系统做成了整块应用,经常出现的情况就是一个小小的改动或者一个小功能的增加可能会引起整体逻辑的修改,造成牵一发而动全身。

通过组件化开发,可以有效实现单独开发,单独维护,而且他们之间可以随意的进行组合。大大提升开发效率低,降低维护成本。

今天给大家介绍的一款组件: 前端Vue uni-appApp/小程序/H5通用tree树形结构图 ,

阅读全文下载完整组件代码请关注微信公众号: 前端组件开发
在这里插入图片描述

效果图如下:

cc-treeChart

使用方法

<!-- 引入lime-echart组件 -->

import LEchart from '@/uni_modules/lime-echart/components/l-echart/l-echart.vue';

import * as echartsLime from '@/uni_modules/lime-echart/static/echarts.min'

export default {

components: {

LEchart

},

}

<l-echart ref="chart" @finished="init"></l-echart>

<!-- 在method实现init方法 -->

async init() {

var fatherColor = 'green';

var midColor = 'rgb(193, 92, 31)';

var smallColor = 'rgb(247, 203, 174)';

// 新能源汽车

let swyyQ = {

"name": "新能源汽车",

itemStyle: {

color: midColor

},

"children": [{

"name": "大型企业",

itemStyle: {

color: fatherColor

},

},

{

"name": "中型企业",

itemStyle: {

color: midColor

},

},

{

"name": "小型企业",

itemStyle: {

color: smallColor

},

},

{

"name": "其他规模企业",

itemStyle: {

color: fatherColor

},

}

]

};

// 生物与新医药

let xclkQ = {

"name": "生物与新医药",

itemStyle: {

color: fatherColor

},

"children": [{

"name": "大型企业",

itemStyle: {

color: fatherColor

},

},

{

"name": "中型企业",

itemStyle: {

color: midColor

},

},

{

"name": "小型企业",

itemStyle: {

color: smallColor

},

},

{

"name": "其他规模企业",

itemStyle: {

color: fatherColor

},

}

]

};;

let data = {

"name": "行业分类",

itemStyle: {

color: fatherColor

},

"children": [swyyQ, xclkQ]

}

// 获取网页宽度 

let width = 360;

let widthSize = 0.039 * width;

if (widthSize > 36) {

widthSize = 36;

}

let heightSize = widthSize * 2.6;

this.option = {

tooltip: {

trigger: 'item',

triggerOn: 'mousemove'

},

series: [{

type: 'tree',

data: [data],

left: '20%',

right: '20%',

top: '16%',

bottom: '32%',

symbol: 'square',

symbolSize: [widthSize, heightSize],

orient: 'vertical',

expandAndCollapse: true,

initialTreeDepth: 2,

label: {

position: 'top',

rotate: 0,

verticalAlign: 'middle',

align: 'center',

fontSize: 12

},

leaves: {

label: {

position: 'bottom',

rotate: -90,

verticalAlign: 'middle',

align: 'left'

}

},

animationDurationUpdate: 150

}]

};

// chart 图表里

const chart = await this.$refs.chart.init(echartsLime);

chart.setOption(this.option)

}

HTML代码实现部分

<template>

<view class="content">

<view class="mui-content" style="margin-top: 16px;">

<l-echart ref="chart" @finished="init"></l-echart>

</view>

</view>

</template>

<script>

import LEchart from '@/uni_modules/lime-echart/components/l-echart/l-echart.vue';

import * as echartsLime from '@/uni_modules/lime-echart/static/echarts.min'

export default {

components: {

LEchart

},

data() {

return {

option: {},

}

},

mounted() {

},

methods: {

async init() {

var fatherColor = 'green';

var midColor = 'rgb(193, 92, 31)';

var smallColor = 'rgb(247, 203, 174)';

// 新能源汽车

let swyyQ = {

"name": "新能源汽车",

itemStyle: {

color: midColor

},

"children": [{

"name": "大型企业",

itemStyle: {

color: fatherColor

},

},

{

"name": "中型企业",

itemStyle: {

color: midColor

},

},

{

"name": "小型企业",

itemStyle: {

color: smallColor

},

},

{

"name": "其他规模企业",

itemStyle: {

color: fatherColor

},

}

]

};

// 新材料行业

let xclkQ = {

"name": "生物与新医药",

itemStyle: {

color: fatherColor

},

"children": [{

"name": "大型企业",

itemStyle: {

color: fatherColor

},

},

{

"name": "中型企业",

itemStyle: {

color: midColor

},

},

{

"name": "小型企业",

itemStyle: {

color: smallColor

},

},

{

"name": "其他规模企业",

itemStyle: {

color: fatherColor

},

}

]

};;

let data = {

"name": "行业分类",

itemStyle: {

color: fatherColor

},

"children": [swyyQ, xclkQ]

}

// 获取网页宽度

let width = 360;

let widthSize = 0.039 * width;

if (widthSize > 36) {

widthSize = 36;

}

let heightSize = widthSize * 2.6;

this.option = {

tooltip: {

trigger: 'item',

triggerOn: 'mousemove'

},

series: [{

type: 'tree',

data: [data],

left: '20%',

right: '20%',

top: '16%',

bottom: '32%',

symbol: 'square',

symbolSize: [widthSize, heightSize],

orient: 'vertical',

expandAndCollapse: true,

initialTreeDepth: 2,

label: {

position: 'top',

rotate: 0,

verticalAlign: 'middle',

align: 'center',

fontSize: 12

},

leaves: {

label: {

position: 'bottom',

rotate: -90,

verticalAlign: 'middle',

align: 'left'

}

},

animationDurationUpdate: 150

}]

};

// chart 图表

const chart = await this.$refs.chart.init(echartsLime);

chart.setOption(this.option)

}

}

}

</script>

<style>

.content {

display: flex;

flex-direction: column;

}

</style>

### uni-app 中实现树形结构的方法 在uni-app中构建树形结构可以通过多种方式来完成,其中一种常见的方式是利用`<view>`组件嵌套以及自定义数据绑定属性。通过Vue.js的数据驱动视图更新机制[^1],可以轻松创建动态的树状UI。 下面是一个简单的例子展示如何使用vue-cli脚手架工具初始化项目并加入基本的树形控件: #### 创建新项目 首先确保安装了Node.js环境之后,在命令行输入如下指令新建一个基于vue-cli模板的uni-app工程: ```bash npm install -g @vue/cli vue create my-tree-project --preset cordova-template-uniapp cd my-tree-project ``` #### 准备树节点模型 为了表示层次关系,通常会设计成具有子项列表的对象数组形式。这里给出一个简化版JSON格式作为示例: ```json [ { "id": 1, "name": "根节点", "children": [ {"id": 2, "name": "子节点A"}, {"id": 3, "name": "子节点B"} ] } ] ``` #### 编写页面逻辑 编辑pages/index/index.vue文件,引入上述准备好的数据源,并编写渲染函数以递归地遍历每一层元素: ```html <!-- pages/index/index.vue --> <template> <scroll-view class="container"> <!-- 调用TreeItem组件传递顶层节点 --> <tree-item :node="root"></tree-item> </scroll-view> </template> <script> import TreeItem from '@/components/TreeItem' export default { components: { TreeItem }, data() { return { root: [/* 插入之前提到过的JSON对象 */] }; } } </script> ``` 接着还需要单独建立一个名为`TreeItem.vue`的小部件用于处理单个节点及其可能存在的后代们: ```html <!-- components/TreeItem.vue --> <template> <block v-for="(item,index) in node.children" :key="index"> <text>{{ item.name }}</text> <!-- 显示当前名称 --> <!-- 如果存在更多层级,则继续调用自己形成闭包效果 --> <tree-item v-if="Array.isArray(item.children)" :node="item"/> </block> </template> <script> export default { props: ['node'] }; </script> ``` 以上就是关于怎样在uni-app里边搭建起基础版本的树型菜单体系的一个简单介绍。当然实际应用场景下还可以考虑增加展开收拢动画、样式美化等功能特性使交互体验更加友好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端组件开发

你的钟意将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值