您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“vue怎么實(shí)現(xiàn)可搜索下拉框功能”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
效果圖:
子組件 DROPDOWN.VUE
<template> <div class="vue-dropdown default-theme"> <div class="cur-name" :class="isShow ? 'show':''" @click="isShow =! isShow">{{itemlist.cur.name}}</div> <div class="list-and-search" :class="isShow?'on':''"> <div class="search-module clearfix" v-show="isNeedSearch"> <input class="search-text" @keyup='search($event)' :placeholder="placeholder" /> </div> <ul class="list-module"> <li v-for ="(item,index) in datalist" @click="selectToggle(item)" :key="index"> <span class="list-item-text">{{item.name}}</span> </li> </ul> <div class="tip-nodata" v-show="isNeedSearch && datalist.length == 0">{{nodatatext}}</div> </div> </div> </template> <script> export default { data(){ return { datalist:[], isShow:false } }, props:{ 'itemlist':Object,//父組件傳來(lái)的數(shù)據(jù) 'placeholder':{ type:String, default: '搜索' //input placeholder的默認(rèn)值 }, 'isNeedSearch':{ //是否需要搜索框 type:Boolean, default: false }, 'nodatatext':{ type:String, default: '未找到結(jié)果' //沒(méi)有搜索到時(shí)的文本提示 } }, created(){ this.datalist = this.itemlist.data; //點(diǎn)擊組件以外的地方,收起 document.addEventListener('click', (e) => { if (!this.$el.contains(e.target)){ this.isShow = false; } }, false) }, methods:{ selectToggle(data){ this.itemlist.cur.name = data.name; this.isShow = false; this.$emit('item-click',data); }, search(e){ let searchvalue = e.currentTarget.value; this.datalist = this.itemlist.data.filter((item,index,arr)=>{ return item.name.indexOf(searchvalue) != -1; }); } } } </script> <style lang="less" scoped> .list-and-search{ background: #fff; border: 1px solid #ccc; display: none; &.on{ display: block; } } .cur-name{ height: 32px; line-height: 32px; text-indent: 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; } } } .vue-dropdown.default-theme { width: 200px; z-index:10; border-radius:3px; border: 1px solid #ccc; cursor: pointer; -webkit-user-select:none; user-select:none; &._self-show { display: block!important; } .search-module { position: relative; border-bottom: 1px solid #ccc; .search-text { width: 100%; height: 30px; text-indent: 10px; // border-radius: 0.5em; box-shadow: none; outline: none; border: none; // &:focus { // border-color: #2198f2; // } } .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; } margin-top: 0.4em; padding: 0.4em; &:hover { cursor:pointer; color: #fff; background: #00a0e9; } } } } .tip-nodata { font-size: 14px; padding: 10px 0; text-indent: 10px; } </style>
父組件調(diào)用
<dropdown :item-click="dropDownClick" :isNeedSearch="true" :itemlist="itemlist"></dropdown>
import Dropdown from '@/components/dropdown.vue' export default { data() { return { itemlist: { cur: { val: "", name: "所有產(chǎn)品" }, data: [{ val: "", name: "所有產(chǎn)品" }, { val: 1, name: "夢(mèng)幻西游" }, { val: 2, name: "夢(mèng)幻無(wú)雙" }, { val: 3, name: "大話(huà)西游" }] }, } }, components: { Dropdown, }, methods :{ dropDownClick(e) { console.log(e.name, e.val) } } }
默認(rèn)是不帶搜索框,如果需要可以傳這個(gè):isNeedSearch="true"。
ps:下面看下vue組件實(shí)踐-可搜索下拉框
實(shí)踐加深對(duì)vue的理解和運(yùn)用有效途徑,本文是基于vue的可搜索下拉框定制組件實(shí)現(xiàn),在此記錄.
一、效果
二、組件代碼
dropdown.vue
<template> <div class="vue-dropdown default-theme" v-show-extend="show"> <div class="search-module clearfix" v-show="itemlist.length"> <input class="search-text" @keyup='search($event)' :placeholder="placeholder" /> <span class="glyphicon glyphicon-search search-icon"></span> </div> <ul class="list-module" v-show="length"> <li v-for ="(item,index) in datalist" @click="appClick(item)" :key="index"> <span class="list-item-text">{{item.name}}</span> </li> </ul> <div class="tip__nodata" v-show="!length">{{nodatatext}}</div> </div> </template> <script> export default { data(){ return { datalist:[] } }, props:{ 'show':{//用于外部控制組件的顯示/隱藏 type:Boolean, default:true }, 'itemlist':Array, 'placeholder':String, 'nodatatext':String }, watch:{ itemlist:function(val){ this.datalist = val.concat(); } }, directives:{ 'show-extend':function(el,binding,vnode){//bind和 update鉤子 let value = binding.value,searchInput = null; if(value){ el.style.display='block'; }else{//隱藏后,恢復(fù)初始狀態(tài) el.style.display='none'; searchInput = el.querySelector(".search-text"); searchInput.value = ''; vnode.context.datalist = vnode.context.itemlist;//還原渲染數(shù)據(jù) } } }, methods:{ appClick:function(data){ this.$emit('item-click',data); }, search:function(e){ let vm = this,searchvalue = e.currentTarget.value; vm.datalist = vm.itemlist.filter(function(item,index,arr){ return item.name.indexOf(searchvalue) != -1; }); } }, computed:{ length:function(){ return this.datalist.length; } } } </script> <style lang="scss" scoped> .vue-dropdown.default-theme { position: absolute; left:15%; display: none; width: 70%; margin: 0 auto; margin-top: 1em; padding: 1em; z-index:10; box-shadow: 0px 0px 10px #ccc; &._self-show { display: block!important; } .search-module { position: relative; .search-text { width: 100%; height: 30px; padding-right: 2em; padding-left:0.5em; border-radius: 0.5em; box-shadow: none; border: 1px solid #ccc; &:focus { border-color: #2198f2; } } .search-icon { position: absolute; top: 24%; right: 0.5em; color: #aaa; } } .list-module { max-height: 200px; overflow-y: auto; li { &._self-hide { display: none; } margin-top: 0.5em; padding: 0.5em; &:hover { cursor:pointer; color: #fff; background: #00a0e9; } } } } .tip__nodata { font-size: 12px; margin-top: 1em; } </style>
三、組件使用
<dropdown :itemlist="itemlist" :placeholder="placeholder" :nodatatext="nodatatext"></dropdown>
“vue怎么實(shí)現(xiàn)可搜索下拉框功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。