vue下拉菜單組件(含搜索)的實(shí)現(xiàn)代碼
之前也寫(xiě)過(guò)這個(gè)小組件,最近遇到select下加搜索的功能,所以稍微完善一下。
效果圖:

子組件 dropdown.vue
<template>
<div class="vue-dropdown default-theme">
<div class="cur-name" @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: "大話西游"
}]
},
}
},
components: {
Dropdown,
},
methods :{
dropDownClick(e) {
console.log(e.name, e.val)
}
}
}
默認(rèn)是不帶搜索框,如果需要可以傳這個(gè) :isNeedSearch="true" 。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue實(shí)現(xiàn)自定義下拉菜單功能
- 詳解Vue用自定義指令完成一個(gè)下拉菜單(select組件)
- vue實(shí)現(xiàn)帶過(guò)渡效果的下拉菜單功能
- vue實(shí)現(xiàn)下拉菜單樹(shù)
- 解決vue動(dòng)態(tài)下拉菜單 有數(shù)據(jù)未反應(yīng)的問(wèn)題
- vue實(shí)現(xiàn)導(dǎo)航欄效果(選中狀態(tài)刷新不消失)
- vue實(shí)現(xiàn)nav導(dǎo)航欄的方法
- vue使用ElementUI時(shí)導(dǎo)航欄默認(rèn)展開(kāi)功能的實(shí)現(xiàn)
- Vue實(shí)現(xiàn)導(dǎo)航欄菜單
- vue實(shí)現(xiàn)導(dǎo)航欄下拉菜單
相關(guān)文章
重新認(rèn)識(shí)vue之事件阻止冒泡的實(shí)現(xiàn)
這篇文章主要介紹了重新認(rèn)識(shí)vue之事件阻止冒泡的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue實(shí)現(xiàn)圖片滾動(dòng)的示例代碼(類(lèi)似走馬燈效果)
下面小編就為大家分享一篇vue實(shí)現(xiàn)圖片滾動(dòng)的示例代碼(類(lèi)似走馬燈效果),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Vue實(shí)現(xiàn)自定義組件改變組件背景色(示例代碼)
要實(shí)現(xiàn) Vue 自定義組件改變組件背景色,你可以通過(guò) props 將背景色作為組件的一個(gè)屬性傳遞給組件,在組件內(nèi)部監(jiān)聽(tīng)這個(gè)屬性的變化,并將其應(yīng)用到組件的樣式中,下面通過(guò)示例代碼介紹Vue如何實(shí)現(xiàn)自定義組件改變組件背景色,感興趣的朋友一起看看吧2024-03-03
vue動(dòng)態(tài)設(shè)置頁(yè)面title的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于vue動(dòng)態(tài)設(shè)置頁(yè)面title的相關(guān)資料,文中通過(guò)實(shí)例代碼結(jié)束的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Vue組件通信$attrs、$listeners實(shí)現(xiàn)原理解析
這篇文章主要介紹了Vue組件通信$attrs、$listeners實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

