iview 远程搜索选择器方法使用,选择之后清空选择的项

vue项目中使用iview远程搜索选择器匹配数据,选择之后,提交数据,继续选择。

<Select v-model="associatedCode" filterable remote :remote-method="search" ref="relation">
	<Option v-for="(item,index) in codeLists" :value="item.out_code" :key="index">{{ item.out_code }}</Option>
</Select>

参数:

filterable 是否支持搜索 Boolean false
remote 是否使用远程搜索 Boolean false
remote-method 远程搜索的方法 Function -
export default {  
    data() {
	return {
            codeLists: [], //远程搜索列表
            active:'', //输入框内容
	    associatedCode: "", //选择的内容
            timer:null,
        }
    },
    methods:{
        search(val) {//搜索
	    clearTimeout(this.timer);
	    this.active = val;
	    this.codeLists=[];
	    const _this = this;
	    this.timer = setTimeout(function() {
	        _this.getCodeList(); // 获取数据的函数
	    }, 500);
	},
        getCodeList() {//获取接口搜索数据
	    this.codeLists= [];
	    this.$axios
	    .get("/code/outrelation", {
		params: this.active,				
	    }).then(res => {
		if(res.success) {
		    for(let i of res.data) {
		        this.codeLists.push(i);
		    }
		}
	    });
	},
        clear(){//清空数据,重新获取列表
            this.associatedCode = '';
	    this.active = '';
	    this.$refs.relation.$data.query = ''; //清除下拉选中的项
            this.getCodeList();
        }
    }
}

发布了94 篇原创文章 · 获赞 42 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/qq_29483485/article/details/101547526