uniapp 树形下拉菜单

<template>
	<view class="selectMain">
		<div class="tagItem" :class="{tagSelect: activeIndex === index}">
			<span class="titText"
				@click.stop="handleClickTag(index,data.id,data.name)">{{ defaultValue? defaultValue: data.name }}</span>
			<div class="iconsBtn" @click.stop="clickIcon(index,data.id,data.name)"
				v-if="data&&data.children&&data.children.length>0">
				<uni-icons v-if="show&&activeIndex === index" custom-prefix="iconfont" type="icon-xialaanniu1"
					color="#fff" size="16"></uni-icons>
				<uni-icons v-else :color="`${activeIndex === index?'#fff': '#000'}`" custom-prefix="iconfont"
					type="icon-xialaanniu" size="16"></uni-icons>
			</div>
			<div class="popeover" v-if="show&&activeIndex === index">
				<div class="optionMain">
					<div v-for="(item,index) in [data]" :key="index">
						<div :class="{select: defaultValue == item.name}" style="display: flex;">
							<span @click.stop="changeChildrenShow">
								<uni-icons custom-prefix="iconfont"
									:type="`${childrenShow?'icon-xialaanniu1':'icon-xialaanniu'}`" size="16"
									style="margin: 0 5px;"></uni-icons>
							</span>
							<span @click.stop="chooseVal(item)">{{ item.name }}</span>
						</div>
						<div v-if="childrenShow">
							<div v-for="(item1,index1) in item.children" :key="index1" class="m2"
								@click.stop="chooseVal(item1)" :class="{select: defaultValue == item1.name}">
								<uni-icons v-if="item1&&item1.children&&item1.children.length>0"
									:type="`${childrenShow1?'up':'down'}`" size="20"></uni-icons>{{item1.name}}

								<div v-if="childrenShow1&&item1&&item1.children&&item1.children.length>0">
									<div v-for="(item2,index2) in item1.children" :key="index2" class="m3">
										{{item2.name}}
									</div>
								</div>

							</div>

						</div>
					</div>
				</div>
			</div>
		</div>
	</view>
</template>

<script>
	export default {
		props: {
			data: {
				type: Object,
				default () {
					return {};
				}
			},
			activeIndex: {
				type: Number,
				default () {
					return;
				}
			},
			index: {
				type: Number,
				default () {
					return;
				}
			},
		},
		data() {
			return {
				show: false,
				childrenShow: true,
				childrenShow1: true,
				defaultValue: ''
			}
		},

		created() {
			this.$nextTick(() => {
				this.defaultValue = this.data.name
			})
		},
		methods: {
			clickIcon(index, code, name) {
				this.show = !this.show

				if (this.show) {
					this.$emit('handleClickTag', index, code, name)
				}
			},
			changeChildrenShow(item) {
				this.childrenShow = !this.childrenShow
			},
			handleClickTag(index, code,name) {
				this.$emit('handleClickTag', index, code, name)
			},
			chooseVal(item) {
				console.log(item)
				this.defaultValue = item.name
				this.show = false
				this.$emit('changeCode', item.id, item.name)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.selectMain {
		width: 100%;
		height: 100%;

		.tagItem {
			box-sizing: border-box;
			padding: 0 10px;
			border-radius: 6px;
			height: 30px;
			line-height: 30px;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: rgba(182, 3, 2, .2);
			margin-right: 10px;
			margin-bottom: 10px;
			position: relative;

			.titText {
				height: 30px;
				line-height: 30px;
			}

			.iconsBtn {
				width: 20px;
				height: 30px;
				line-height: 30px;
				margin-left: 5px;
			}

			.popeover {
				position: absolute;
				left: 0;
				top: 35px;
				min-width: 100%;
				min-height: 100px;
				max-height: 60vh;
				overflow-y: auto;
				background-color: #fff;
				z-index: 99;
				box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
				color: #000;

				.optionMain {
					box-sizing: border-box;
					padding: 0 10px;
					white-space: nowrap;

					.m2 {
						max-width: 180px;
						box-sizing: border-box;
						padding-left: 40px;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
						-o-text-overflow: ellipsis;

					}

					.m3 {
						max-width: 150%;
						box-sizing: border-box;
						padding-left: 60px;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
						-o-text-overflow: ellipsis;
					}

					.select {
						background-color: rgba(182, 3, 2, .1);

					}
				}
			}
		}

		.tagSelect {
			background-color: #e64340;
			color: #fff;
		}

	}
</style>

多层树需要添加 循环   

icon引入iconfont (具体操作可参考uniapp官网 uni-icons 图标 | uni-app官网

功能不全 目前只是初步实现 仅供参考提供个思路

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值