最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue實現(xiàn)typeahead組件功能(非??孔V)

 更新時間:2017年08月26日 16:18:42   作者:crper  
本文給大家分享通過Vue寫一個挺靠譜的typeahead組件功能,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧

 前言

之前那個typeahead寫的太早,不滿足當前的業(yè)務需求。

而且有些瑕疵,還有也不方便傳入數(shù)據(jù)和響應數(shù)據(jù)..

于是就推倒了重來,寫了個V2的版本

看圖,多了一些細節(jié)的考慮;精簡了實現(xiàn)的邏輯代碼

效果圖

這里寫圖片描述

實現(xiàn)的功能

1: 鼠標點擊下拉框之外的區(qū)域關閉下拉框

2: 支持鍵盤上下鍵選擇,支持鼠標選擇

3: 支持列表過濾搜索

4: 支持外部傳入列表JSON格式的映射

5: 支持placeholder的傳入

6: 選中對象的響應(.sync vue2.3的組件通訊的語法糖)

7: 箭頭icon的映射,感覺作用不大,移除了

用法

<select-search 
style="max-width:195px" 
placeholder="請選擇廣告主" 
:asyncData.sync="adHostData" 
:mapData="adHostDataList" 
:mapDataFormat="{label:'userName',value:'userId'}">
</select-search>
  • asyncData:響應的數(shù)據(jù),也就是選中的..回來是一個對象
  • mapData : 搜索的列表數(shù)據(jù),肯定是外部傳入了…
  • mapData : 列表值映射

代碼

selectSearch.vue

<template>
 <div class="select-search" v-if="typeaheadData" ref="selectSearch" @click.native="showHideMenu($event)">
  <div class="select-header">
   <input type="text" autocomplete="off" readonly :placeholder="placeholder" :value="placeholderValue" @keydown.down.prevent="selectChildWidthArrowDown" @keydown.up.prevent="selectChildWidthArrowUp" @keydown.enter="selectChildWidthEnter">
   <i class="fzicon " :class="isExpand?'fz-ad-jiantou1':'fz-ad-jiantou'"></i>
  </div>
  <div class="select-body" v-if="isExpand && typeaheadData">
   <input type="text" placeholder="關鍵字" v-model="searchVal" autocomplete="off" @keydown.esc="resetDefaultStatus" @keydown.down.prevent="selectChildWidthArrowDown" @keydown.up.prevent="selectChildWidthArrowUp" @keydown.enter="selectChildWidthEnter">
   <transition name="el-fade-in-linear" mode="out-in">
    <div class="typeahead-filter">
     <transition-group tag="ul" name="el-fade-in-linear" v-show="typeaheadData.length>0">
      <li v-for="(item,index) in typeaheadData" :key="index" :class="item.active ? 'active':''" @mouseenter="setActiveClass(index)" @mouseleave="setActiveClass(index)" @click="selectChild(index)">
       <a href="javascript:;" rel="external nofollow" >
        {{item[mapDataFormat.label]}}
       </a>
      </li>
     </transition-group>
     <p class="noFound" v-show="typeaheadData && typeaheadData.length === 0">未能查詢到,請重新輸入!</p>
    </div>
   </transition>
  </div>
 </div>
</template>
<script>
 export default {
  name: 'selectSearch',
  data: function () {
   return {
    placeholderValue: '',// 給看到選擇內(nèi)容的
    isExpand: false,
    searchVal: '', // 搜索關鍵字
    resultVal: '', // 保存搜索到的值
    searchList: [], //保存過濾的結果集
    currentIndex: -1, // 當前默認選中的index,
   }
  },
  computed: {
   mapFormatData () { // 外部有傳入格式的時候映射mapData
    return this.mapData.map(item => {
     item[this.mapDataFormat.value] = item[this.mapDataFormat.value];
     return item;
    });
   },
   typeaheadData () {
    let temp = [];
    if (this.searchVal && this.searchVal === '') {
     return this.mapFormatData;
    } else {
     this.currentIndex = -1; // 重置特殊情況下的索引
     this.mapFormatData.map(item => {
      if (item[this.mapDataFormat.label].indexOf(this.searchVal.toLowerCase().trim()) !== -1) {
       temp.push(item)
      }
      return item;
     })
     return temp;
    }
   }
  },
  props: {
   placeholder: {
    type: String,
    default: '--請選擇--'
   },
   emptyText: {
    type: String,
    default: '暫無數(shù)據(jù)'
   },
   mapData: { // 外部傳入的列表數(shù)據(jù)
    type: Array,
    default: function () {
     return []
    }
   },
   mapDataFormat: { // 映射傳入數(shù)據(jù)的格式
    type: Object,
    default: function () {
     return {
      label: 'text',
      value: 'value',
      extraText: 'extraText'
     }
    }
   },
   asyncData: { // 實時響應的值
    type: [Object, String],
    default: function () {
     return {}
    }
   }
  },
  methods: {
   showHideMenu (e) { // 點擊其他區(qū)域關閉下拉列表
    if (e) {
     if (this.$refs.selectSearch && this.$refs.selectSearch.contains(e.target)) {
      this.isExpand = true;
     } else {
      this.isExpand = false;
     }
    }
   },
   resetDefaultStatus () { // 清除所有選中狀態(tài)
    this.searchVal = '';
    this.currentIndex = -1;
    this.typeaheadData.map(item => {
     this.$delete(item, 'active');
    })
   },
   setActiveClass (index) { // 設置樣式活動類
    this.typeaheadData.map((item, innerIndex) => {
     if (index === innerIndex) {
      this.$set(item, 'active', true);
      this.currentIndex = index; // 這句話是用來修正index,就是鍵盤上下鍵的索引,不然會跳位
     } else {
      this.$set(item, 'active', false)
     }
    })
   },
   selectChildWidthArrowDown () {
    // 判斷index選中子項
    if (this.currentIndex < this.typeaheadData.length) {
     this.currentIndex++;
     this.typeaheadData.map((item, index) => {
      this.currentIndex === index ? this.$set(item, 'active', true) : this.$set(item, 'active', false);
     })
    }
   },
   selectChildWidthArrowUp () {
    // 判斷index選中子項
    if (this.currentIndex > 0) {
     this.currentIndex--;
     this.typeaheadData.map((item, index) => {
      this.currentIndex === index ? this.$set(item, 'active', true) : this.$set(item, 'active', false);
     })
    }
   },
   selectChildWidthEnter () {
    // 若是結果集只有一個,則默認選中
    if (this.typeaheadData.length === 1) {
     this.$emit('update:asyncData', this.typeaheadData[0]); // emit響應的值
     this.placeholderValue = this.typeaheadData[0][this.mapDataFormat.label];
    } else {
     // 若是搜索的內(nèi)容完全匹配到項內(nèi)的內(nèi)容,則默認選中
     this.typeaheadData.map(item => {
      if (this.searchVal === item[this.mapDataFormat.label] || item.active === true) {
       this.$emit('update:asyncData', item); // emit響應的值
       this.placeholderValue = item[this.mapDataFormat.label];
      }
     })
    }
    this.isExpand = false;
   },
   selectChild (index) {
    // 鼠標點擊選擇子項
    this.typeaheadData.map((item, innerIndex) => {
     if (index === innerIndex || item.active) {
      this.placeholderValue = item[this.mapDataFormat.label];
      this.$emit('update:asyncData', item); // emit響應的值
     }
    });
    this.isExpand = false;
   },
  },
  mounted () {
   window.addEventListener('click', this.showHideMenu);
  },
  beforeDestroy () {
   window.removeEventListener('click', this.showHideMenu);
  },
  watch: {
   'isExpand' (newValue) {
    if (newValue === false) {
     this.resetDefaultStatus();
    }
   }
  }
 }
</script>
<style scoped lang="scss">
 .el-fade-in-linear-enter-active,
 .el-fade-in-linear-leave-active,
 .fade-in-linear-enter-active,
 .fade-in-linear-leave-active {
  transition: opacity .2s linear;
 }
 .el-fade-in-enter,
 .el-fade-in-leave-active,
 .el-fade-in-linear-enter,
 .el-fade-in-linear-leave,
 .el-fade-in-linear-leave-active,
 .fade-in-linear-enter,
 .fade-in-linear-leave,
 .fade-in-linear-leave-active {
  opacity: 0;
 }
 .noFound {
  text-align: center;
 }
 .select-search {
  position: relative;
  z-index: 1000;
  a {
   color: #333;
   text-decoration: none;
   padding: 5px;
  }
  ul {
   list-style: none;
   padding: 6px 0;
   margin: 0;
   max-height: 200px;
   overflow-x: hidden;
   overflow-y: auto;
   li {
    display: block;
    width: 100%;
    padding: 5px;
    font-size: 14px;
    padding: 8px 10px;
    position: relative;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: #48576a;
    height: 36px;
    line-height: 1.5;
    box-sizing: border-box;
    cursor: pointer;
    &.active {
     background-color: #20a0ff;
     a {
      color: #fff;
     }
    }
   }
  }
  .select-header {
   position: relative;
   border-radius: 4px;
   border: 1px solid #bfcbd9;
   outline: 0;
   padding: 0 8px;
   >input {
    border: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    width: 100%;
    outline: 0;
    box-sizing: border-box;
    color: #1f2d3d;
    font-size: inherit;
    height: 36px;
    line-height: 1;
   }
   >i {
    transition: all .3s linear;
    display: inline-block;
    position: absolute;
    right: 3%;
    top: 50%;
    transform: translateY(-50%);
   }
  }
  .select-body {
   position: absolute;
   border-radius: 2px;
   background-color: #fff;
   box-sizing: border-box;
   margin: 5px 0;
   padding: 8px;
   width: 100%;
   box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
   >input {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #bfcbd9;
    box-sizing: border-box;
    color: #1f2d3d;
    font-size: inherit;
    height: 36px;
    line-height: 1;
    outline: 0;
    padding: 3px 10px;
    transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
    width: 100%;
    display: inline-block;
    &:focus {
     outline: 0;
     border-color: #20a0ff;
    }
   }
  }
 }
</style>

總結

以上所述是小編給大家介紹的Vue實現(xiàn)typeahead組件功能(非??孔V),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • Vue+ElementUI容器無法鋪滿網(wǎng)頁的問題解決

    Vue+ElementUI容器無法鋪滿網(wǎng)頁的問題解決

    這篇文章主要介紹了Vue+ElementUI容器無法鋪滿網(wǎng)頁的問題解決,文章通過圖文結合的方式給大家講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-08-08
  • 基于vue實現(xiàn)swipe輪播組件實例代碼

    基于vue實現(xiàn)swipe輪播組件實例代碼

    本篇文章主要介紹了基于vue實現(xiàn)swipe輪播組件實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • vue中push()和splice()的使用解析

    vue中push()和splice()的使用解析

    這篇文章主要介紹了vue中push()和splice()的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vite的常見配置選項詳細說明

    Vite的常見配置選項詳細說明

    相信大部分兄弟都體驗過Vite了,都知道它很快,在學習的時候,官網(wǎng)上的各種配置也是看的眼花繚亂,不知道哪些是必要掌握的,下面這篇文章主要給大家介紹了關于Vite常見配置選項的相關資料,需要的朋友可以參考下
    2024-09-09
  • vue2中filter()的實現(xiàn)代碼

    vue2中filter()的實現(xiàn)代碼

    vue2.0里,不再有自帶的過濾器,需要自己定義過濾器。下面通過實例代碼給大家介紹vue2中filter()的相關知識,感興趣的朋友一起看看吧
    2017-07-07
  • 如何在vue中使用HTML 5 拖放API

    如何在vue中使用HTML 5 拖放API

    這篇文章主要介紹了如何在vue中使用HTML 5 拖放API,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2021-01-01
  • 使用Vue純前端實現(xiàn)發(fā)送短信驗證碼并實現(xiàn)倒計時

    使用Vue純前端實現(xiàn)發(fā)送短信驗證碼并實現(xiàn)倒計時

    在實際的應用開發(fā)中,涉及用戶登錄驗證、密碼重置等場景時,通常需要前端實現(xiàn)發(fā)送短信驗證碼的功能,以提升用戶體驗和安全性,以下是一個簡單的前端實現(xiàn),演示了如何在用戶點擊發(fā)送驗證碼按鈕時觸發(fā)短信驗證碼的發(fā)送,并開始一個倒計時
    2024-04-04
  • vue3+elementplus前端生成圖片驗證碼完整代碼舉例

    vue3+elementplus前端生成圖片驗證碼完整代碼舉例

    在開發(fā)過程中有時候需要使用圖片驗證碼進行增加安全強度,在點擊圖片時更新新的圖片驗證碼,記錄此功能,以便后期使用,這篇文章主要給大家介紹了關于vue3+elementplus前端生成圖片驗證碼的相關資料,需要的朋友可以參考下
    2024-03-03
  • vue通過vue-lazyload實現(xiàn)圖片懶加載的代碼詳解

    vue通過vue-lazyload實現(xiàn)圖片懶加載的代碼詳解

    這篇文章主要給大家介紹了vue通過vue-lazyload實現(xiàn)圖片懶加載,文中通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • 詳解vue組件開發(fā)腳手架

    詳解vue組件開發(fā)腳手架

    本篇文章給大家詳細分析了vue組件開發(fā)腳手架的相關內(nèi)容以及知識點,對此有興趣的朋友可以學習參考下。
    2018-06-06

最新評論

竹北市| 澎湖县| 兴国县| 囊谦县| 乐平市| 洛隆县| 中超| 乌鲁木齐市| 鲁甸县| 元江| 久治县| 彰化县| 临海市| 兴和县| 监利县| 平湖市| 松原市| 新蔡县| 元谋县| 马山县| 闽清县| 平和县| 嵩明县| 海安县| 枞阳县| 廉江市| 五家渠市| 泰安市| 邻水| 砚山县| 万年县| 大英县| 紫阳县| 安塞县| 三门峡市| 太白县| 宜宾市| 上虞市| 秦皇岛市| 榆林市| 兖州市|