uni-app微信小程序创建树形组件
目录
1:前言
树形组件应用很广,应用于一些分层次的内容。vue有树形组件,但是uniapp没有树形组件,想要在uniapp使用树形组件,必须要自己写个树形组件。
2:最终效果:
2.1一级菜单
2.2二级菜单
2.3三级菜单
3:实现方法
3.1:创建一级菜单
建立一个view标签,遍历数据
-
<view class="">
-
<view class="" v-for="(item,index) in list" >
-
<view>{{item.title}}</view>
-
</view>
-
</view>
之后在前面添加两个图标,两个图标分别对应点击前和点击后的不同的图标。view绑定方法,点击view时改变状态。使用v-if根据点击获取到的状态判断展示哪个图标。同时使用flex样式使图片和文字水平排列,并设置图片大小
-
<view class="" v-for="(item,index) in list" >
-
<view class="fold" @click="unfold(index)">
-
<view class="tree-icon" v-if="item.fold">
-
<image src="../../static/u.png"class="img" ></image>
-
-
-
</view>
-
<view class="tree-icon" v-else>
-
-
-
<image src="../../static/down.png"class="img" ></image>
-
</view>
-
<view>{{item.title}}</view>
-
</view>
-
-
</view>
-
<script>
-
export default{
-
data(){
-
return{
-
list:[
-
{
-
id:1,
-
-
title:'七年级',
-
fold:false
-
},
-
{
-
id:2,
-
-
title:'八年级',
-
fold:false
-
},
-
{
-
id:3,
-
-
title:'九年级',
-
fold:false
-
}
-
],
-
}
-
},
-
methods:{
-
unfold(index){
-
this.list[index].fold=!this.list[index].fold
-
}
-
}
-
}
-
</script>
css样式如下:
.img{ width: 20px; height: 20px; } .fold{ display: flex; }
最终效果如下(设置好看的样式自己改):
3.2创建二级三级菜单
使用同样方法创建二级三级目录,使用v-if根据点击状态判断是否展示。
效果如下
4:完整代码如下
-
<template>
-
<view class="">
-
-
<view class="" v-for="(item,index) in list" >
-
<view class="fold" @click="unfold(index)">
-
<view class="tree-icon">
-
<view class="" v-if="item.fold">
-
<image src="../../static/u.png"class="img" ></image>
-
-
-
</view>
-
<view class="tree-icon" v-else>
-
-
-
<image src="../../static/down.png"class="img" ></image>
-
</view>
-
<view>{{item.title}}</view>
-
</view>
-
-
</view>
-
<view class="" v-for="(item1,index1) in item.children" v-if="item.fold" @click="foldChildren(index,index1)">
-
<view class="fold1">
-
<view class="tree1">
-
<image src="../../static/d.png" class="img" ></image>
-
-
-
</view>
-
<view class="tree-content">
-
<text>{{item1.title}}</text>
-
</view>
-
</view>
-
-
<view class="fold1" v-for="(item2,index2) in item1.children" v-if="item1.fold" >
-
<view class="tree1">
-
-
-
<u-icon class="icon-children" name="minus-circle" size="24.5px" color="#C0C0C0"></u-icon>
-
</view>
-
<view>
-
<text>{{item2.title}}</text>
-
</view>
-
-
</view>
-
-
</view>
-
-
-
-
</view>
-
</view>
-
</template>
-
-
<script>
-
export default{
-
data(){
-
return{
-
list:[
-
{
-
id:1,
-
-
title:'七年级',
-
fold:false,
-
children:[
-
{
-
title:'第一单元',
-
fold:false,
-
children:[
-
{
-
title:'密度基础'
-
},
-
{
-
title:'密度计算'
-
}
-
]
-
}
-
]
-
},
-
{
-
id:2,
-
-
title:'八年级',
-
fold:false,
-
children:[
-
{
-
title:'第一单元',
-
fold:false,
-
children:[
-
{
-
title:'密度基础'
-
},
-
{
-
title:'密度计算'
-
}
-
]
-
},
-
{
-
title:'第二单元',
-
fold:false
-
},
-
{
-
title:'第三单元',
-
fold:false
-
},
-
]
-
},
-
{
-
id:3,
-
-
title:'九年级',
-
fold:false
-
}
-
],
-
}
-
},
-
methods:{
-
unfold(index){
-
this.list[index].fold=!this.list[index].fold
-
},
-
foldChildren(index,index1){
-
-
this.list[index].children[index1].fold=!this.list[index].children[index1].fold
-
}
-
}
-
}
-
</script>
-
-
<style>
-
.tree-icon{
-
display: flex;
-
}
-
.img{
-
width: 20px;
-
height: 20px;
-
}
-
.fold{
-
-
}
-
.fold1{
-
display: flex;
-
}
-
</style>
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgfjcci
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01