溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用vue怎么實(shí)現(xiàn)下拉菜單組件

發(fā)布時(shí)間:2021-06-01 17:56:54 來源:億速云 閱讀:596 作者:Leah 欄目:web開發(fā)

這篇文章給大家介紹使用vue怎么實(shí)現(xiàn)下拉菜單組件,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

子組件 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,//父組件傳來的數(shù)據(jù)
   'placeholder':{
    type:String,
    default: '搜索' //input placeholder的默認(rèn)值
   },
   'isNeedSearch':{ //是否需要搜索框
    type:Boolean,
    default: false
   },
   'nodatatext':{ //是否需要顯示搜索
    type:String,
    default: '未找到結(jié)果' //沒有搜索到時(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: "夢幻西游"
  }, {
   val: 2,
   name: "夢幻無雙"
  }, {
   val: 3,
   name: "大話西游"
  }]
  },
 }
 },
 components: {
 Dropdown,
 },
 methods :{
 dropDownClick(e) {
  console.log(e.name, e.val)
 }
 }
}

關(guān)于使用vue怎么實(shí)現(xiàn)下拉菜單組件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI