vue組件實(shí)現(xiàn)可搜索下拉框擴(kuò)展
本文實(shí)例為大家分享了vue組件實(shí)現(xiàn)可搜索下拉框的具體代碼,供大家參考,具體內(nèi)容如下
一、效果

二、代碼
dropdown-ext.vue
<template>
<div class="vue-dropdown-ext" :class="themestyle" 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" :style="{maxHeight:maxH+'px'}">
<li v-for ="(item,index) in datalist" @click="appClick(item,$event)"
:key="index" :title="item.name">
<span v-if="addIcon" :class="iconClass"></span>
:style="itemTextStyle">{{item.name}}</span>
<span v-if="statusIconType == 'text' && hasStatus"
:class="item.statusClass">{{item.statusText}}</span>
:class="item.statusClass"></span>
</li>
</ul>
<div class="tip__nodata" v-show="!length&&itemlist.length">
{{nodatatext}}
</div>
</div>
</template>
<script>
export default {
data(){
return {
datalist:[]
}
},
props:{
'show':{//用于外部控制組件的顯示/隱藏
type:Boolean,
default:true
},
'itemlist':Array,
'placeholder':String,
'nodatatext':String,
'themestyle':{
type:String,
default:'default-theme'
},
'item-text-style':{
type:Object,
default:function(){
return {
width:'80%'
}
}
},
'add-icon':{
type:Boolean,
default:true
},
'icon-class':{
type:String,
default:''
},
'has-status':{
type:Boolean,
default:false
},
'status-icon-type':{
type:String,
default:'text'//text or icon
},
'max-h':{
type:Number,
default:$(window).height()-400
}
},
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 = '';
//還原渲染數(shù)據(jù)
vnode.context.datalist = vnode.context.itemlist;
}
}
},
methods:{
appClick:function(data,event){
this.$emit('item-click',data,event);
},
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;
});
},
statusIconClass:function(status){
let statusClass = '';
return statusClass;
}
},
computed:{
length:function(){
return this.datalist.length;
}
}
}
</script>
<style lang="scss" scoped>
.text-overflow__style {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.vue-dropdown-ext {
.search-module { position: relative;
.search-text { width: 100%;
height: 30px;
padding-right: 2em;
padding-left:0.5em;
box-shadow: none;
border: 1px solid #ccc;
background: #fff;
&:focus { border-color: #2198f2;
}
}
.search-icon {
position: absolute;
top: 24%;
right: 0.5em;
color: #aaa;
}
}
.list-module {
overflow: auto;
li { position: relative;
margin-top: 0.5em;
padding: 0.5em;
border: 1px solid #ccc;
white-space: nowrap;
&>span { display: inline-block;
vertical-align: middle;
}
}
}
.tip__nodata {
font-size: 12px;
margin-top: 1em;
}
&.default-theme {
.list-module li { &:hover { cursor: pointer;
border-color: #00a0e9;
}
&.active {
border-color: #00a0e9;
color: #00a0e9;
}
}
}
}
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue 3 中使用 Element Plus 的 `el-t
在 Vue 3 中使用 Element Plus 的 `el-table` 組件實(shí)現(xiàn)自適應(yīng)高度,你可以根據(jù)容器的高度動(dòng)態(tài)設(shè)置表格的高度,下面通過(guò)示例代碼給大家展示,感興趣的朋友一起看看吧2024-12-12
Vue實(shí)現(xiàn)點(diǎn)擊當(dāng)前行變色
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)點(diǎn)擊當(dāng)前行變色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
nginx如何配置vue項(xiàng)目history的路由模式(非根目錄)
這篇文章主要介紹了nginx如何配置vue項(xiàng)目history的路由模式(非根目錄),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
詳解Vue.js使用Swiper.js在iOS<11時(shí)出現(xiàn)錯(cuò)誤
這篇文章主要介紹了詳解Vue.js使用Swiper.js在iOS<11時(shí)出現(xiàn)錯(cuò)誤,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue3.0使用taro-ui-vue3引入組件不生效的問(wèn)題及解決
這篇文章主要介紹了vue3.0使用taro-ui-vue3引入組件不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
關(guān)于vue2響應(yīng)式缺陷的問(wèn)題
這篇文章主要介紹了關(guān)于vue2響應(yīng)式缺陷的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
vue前端和Django后端如何查詢一定時(shí)間段內(nèi)的數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于vue前端和Django后端如何查詢一定時(shí)間段內(nèi)的數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

