微信小程序 实现 树形菜单其实很简单

话不多说,上代码 ➡️

组件 资源下载(想直接下载组件,可以点击下载)

创建一个名为 treeList 的组件

组件代码

treeList.wxml:

<view class="treeList c{{step}}">
    <view class="line" style="margin-left:{{(step-1)*40}}px;"></view>
    <view class="title" style="margin-left:{{(step-1)*40}}px;">
        <view class="icon" wx:if="{{listData.children.length>0 && isShowChildren}}" catchtap="toggleShowChildren">-
        </view>
        <view class="icon" wx:elif="{{listData.children.length<=0}}">
            <view class="dot"></view>
        </view>
        <view class="icon" wx:else catchtap="toggleShowChildren">+</view>
        <view class="text" >{{listData.title}}</view>
    </view>
    <block wx:if="{{listData.children.length>0&&isShowChildren}}" wx:for="{{listData.children}}" wx:key="index">
        <treeList listData="{{item}}" step="{{step+1}}"
            bindtreeListEvent="treeListEvent"></treeList>
    </block>
</view>

treeList.wxss:

.treeList {
	box-sizing: border-box;
	position: relative;
	font-size: 30rpx;
	margin: 20rpx 0;
}
.line {
	position: absolute;
	height: calc(100% - 30rpx) ;
	width: 0px;
	border-left: 2rpx dashed #999999;
	left: 20rpx;
	top: 46rpx;
}
treeList:last-of-type>.treeList>.line {
	display: none;
}
.title {
	display: flex;
	align-items: center;
}
.title .icon {
	height: 38rpx;
	width: 38rpx;
	line-height: 38rpx;
	text-align: center;
	margin-right: 10rpx;
	border: 2rpx solid #cccccc;
	color: #cccccc;
}
.title .icon .dot {
	width: 20rpx;
	height: 20rpx;
	background: #f0f0f0;
	margin: 9rpx 0 0 9rpx;
	border-radius: 50%;
}
.title text {
	margin-right: 6rpx;
}
.title text.iconfont {
	font-size: 42rpx;
}
.title text.icon-tianjia {
	margin-left: 6rpx;
	font-size: 36rpx;
}
.title .btn {
	padding: 8rpx 20rpx;
	border: 2rpx solid #555555;
	color: #555555;
	font-size: 24rpx;
	margin-left: 4rpx;
	border-radius: 8rpx;
}
.title .text {
	padding: 6rpx 10rpx;
	border-radius: 10rpx;
	text-align: center;
	box-sizing: border-box;
}

treeList.json:

{
	"component": true,
	"usingComponents": { "treeList":"/components/treeList/treeList" } 
}

treeList.js:

Component({ properties: { listData:{ type:Array|Object, value:{} }, step:{ type:Number, value:1 }, }, data: { isShowChildren:true, }, methods: {  toggleShowChildren(){ this.setData({ isShowChildren:!this.data.isShowChildren }) }, } })

组件路径根据实际情况

需要使用树形菜单的页面 引入组件

页面代码

json 引用组件

{
  "usingComponents": {
    "treeList":"/components/treeList/treeList"
  },
  "navigationBarTitleText": "标题"
}

wxml中使用组件:

<view class="list">
        <block wx:for="{{listData}}" wx:key="index">
            <treeList listData="{{item}}" step="1" ></treeList>
        </block>
</view>

listData的数据格式如下:

listData:[
            {
                title:'A层级菜单1',
                children:[
                    {
                        title:'B层级菜单1',
                        children:[],
                        isBind:true
                    },
                    {
                        title:'B层级菜单2',
                        children:[
                            {
                                title:'C层级菜单1',
                                children:[]
                            }
                        ]
                    }
                ]
            },
            {
                title:'A层级菜单2',
                children:[]
            }
        ],

展示效果

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值