VUE下拉选择搜索菜单组件

效果展示: 

组件代码: 

<template>
	<div class="vue-dropdown">
		<div class="cur-name" @click="isShow =! isShow">{{current}}</div>
		<div class="list-and-search" :class="isShow?'on':''">
			<div class="search-module clearfix" v-show="isNeedSearch">
				<input class="search-text" @input.sync='search' :placeholder="placeholder" />
			</div>
			<ul class="list-module">
				<li v-for ="(item,index) in datalist" @click="clickItem(item)" :key="index">
					<span class="list-item-text">{{item}}</span>
				</li>
			</ul>
			<div class="tip-nodata" v-show="searchValue.length && !datalist.length">未找到数据</div>
		</div>
	</div>
</template>

<script>
 export default {
  data(){
		return {
			searchValue: '',
			current: this.curData,
			datalist:[],
			isShow:false
		}
  },
  props:{
		curData:String,	// 当前选中数据
		itemData:Array,	// 所有选项数据
		isNeedSearch:{	// 是否需要搜索
			type: Boolean,
			default: true
		},
		placeholder:{	// 输入框提示文本
			type: String,
			default: '搜索' 
		},
  },
  created(){
		this.datalist = this.itemData;
		//点击组件以外的地方,收起
		// document.addEventListener('click', (e) => {
		// 	if (!this.$el.contains(e.target)){
		// 		this.isShow = false; 
		// 	}
		// }, false)
  },
  methods:{
		search(e){
			this.searchValue = e.target.value;
			this.datalist = this.itemData.filter((item)=>{
				return item.indexOf(this.searchValue) != -1;
			});
		},
		clickItem(item){
			this.current = item;
			this.isShow = false;
			this.$emit('change',item);
		},
	}
}
</script>

<style scoped lang="scss">
	.vue-dropdown {
		z-index:10;
		width: 40vw;
		border-radius:3px; 
		border: 1px solid #ccc;
		cursor: pointer;
		user-select:none;
		-webkit-user-select:none; 
		position: relative;
		&._self-show {
			display: block!important;
		}
		.search-module {
			position: relative;
			border-bottom: 1px solid #ccc;
			.search-text {
				width: 100%;
				height: 30px;
				padding: 0 10px;
				box-shadow: none;
				outline: none;
				border: none;
			}
			.search-icon {
				position: absolute;
				top: 24%;
				right: 0.5em;
				color: #aaa;
			}
		}
		input::-webkit-input-placeholder{
			font-size: 14px;
		}
		.list-module {
		 max-height: 200px;
		 overflow-y: auto;
			li {
				&._self-hide {
					display: none;
				}
				padding: 10px;
				&:hover {
					cursor:pointer;
					color: #fff;
					background: #00a0e9;
				}
			}
		}
	}
	.list-and-search{
		display: none;
		background: #fff;
		border: 1px solid #ccc;
		position: absolute;
		width: 100%;
		&.on{
			display: block;
		}
	}
	.cur-name{
		height: 32px;
		line-height: 32px;
		padding: 0 10px;
		position: relative;
		color: #777;
		&:after{
			position: absolute;
			right: 9px;
			top: 13px;
			content: " ";
			width: 0;
			height: 0;
			border-right: 6px solid transparent;
			border-top: 6px solid #7b7b7b;
			border-left: 6px solid transparent;
			border-bottom: 6px solid transparent;
		}
		&.show{
			&:after{
				right: 9px;
				top: 6px;
				border-right: 6px solid transparent;
				border-bottom: 6px solid #7b7b7b;
				border-left: 6px solid transparent;
				border-top: 6px solid transparent;
			}
		}
	}
	.tip-nodata {
		font-size: 14px;
		padding: 10px;
	}
</style>

父组件引用:

<selectSearch @change="dropDownClick" :curData="curData" :itemData="itemData"></selectSearch>
import selectSearch from '@/component/select-search.vue'
export default {
	components: {
		selectSearch,
	},
	data() {
		return {
			curData: '所有产品',
			itemData: ['所有产品','梦幻西游','梦幻无双','大话西游'],
		};
	},
    methods: {
	    dropDownClick(e) {
		    console.log(e)
        }
	},

}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值