VUE 下拉菜单动态获取

时间:2024-10-22 08:58:18
子组件:
<template>
	<div>
	<input class="form-control autoSelect" type="text" @mouseup="mouse" @keyup="loadOptions" v-model="selectContent" />
	<div class="autoSelectDiv" isHover="false" v-show="isShow">
		<ul>
			<li v-for="dataItem in selectList" @click="selectItem(dataItem)">{{}}</li>
		</ul>
	</div>
	</div>
</template>
<script type="text/javascript">
	export default{
		name:'autoSelect',
		props:{ 
                        //父级传过来的默认被选中的值
			name:{
				type:String,
				default:''
			},
			url:{
				type: String,
            			default:''
			}
                },
		data(){
			return{								
				returnContent:{selectedId:'',flag:0,selectContent:''},//我多个地方调用了下拉菜单,所以加了一个flag区分		
				selectList:[],
				isShow:false
			}
		},
		mounted(){
			//点击其他地方,隐藏下拉菜单
			=(e)=>{
					if(==true){
					=false;
				}
			}
			('click',);
		},
		watch:{
			name:{
				handler:function (newValue, oldValue) {
					=newValue;
				}
			}
		},
		methods:{			
			selectItem(data){
				=;
				=;
				=false;
				this.$emit('getAutoSelectResult',);
			},
			mouse(){  //点击输入框时就发送请求
				if(==""){
					();
				}
			},
			loadOptions(){
				var _this=this;var parameter={};
				switch(_this.url){
					case 'url1':
						parameter['param1']='要传的参数1';
						=1;
						break;
					case 'url2':
						parameter['param2']='要传的参数2';
						=2;
						break;
						
				}
				parameter['name']=;
				//请求接口去吧
				doRequest(url,parameter,function(result){					
					for(var i=0;i<;i++){						
						_this.({vCode:obj[i].value,vName:obj[i].name});
					}
					_this.isShow=true;
				})
			}
		}
	}

父组件:

<autoSelect :url="要请求的url" :name="要默认显示的值" @getAutoSelectResult="listenAutoSelectResult"></autoSelect>




子组件: