vue组件实现可搜索下拉框扩展

作者:wl_ 时间:2024-05-28 15:41:57 

本文实例为大家分享了vue组件实现可搜索下拉框的具体代码,供大家参考,具体内容如下

一、效果

vue组件实现可搜索下拉框扩展

二、代码

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{//隐藏后,恢复初始状态
         el.style.display='none';
         searchInput = el.querySelector(".search-text");
         searchInput.value = '';
         //还原渲染数据
         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>

来源:https://blog.csdn.net/wanglei1991gao/article/details/80579666

标签:vue,搜索,下拉框
0
投稿

猜你喜欢

  • 读写xml文件的2个小函数

    2007-08-23 12:59:00
  • Python实现企业微信通知机器人的方法详解

    2021-03-12 09:56:02
  • 利用Python分析一下最近的股票市场

    2023-06-14 23:46:03
  • 详解python列表生成式和列表生成式器区别

    2021-09-13 16:15:14
  • vue.js实现只弹一次弹框

    2024-05-09 15:08:07
  • C#连接SQL数据库和查询数据功能的操作技巧

    2024-01-19 03:31:03
  • ASP生成数字相加求和的BMP图片验证码

    2011-04-14 10:48:00
  • 详解Python读取和写入操作CSV文件的方法

    2021-03-01 23:13:43
  • 在pycharm中使用pipenv创建虚拟环境和安装django的详细教程

    2021-06-06 19:02:37
  • Pytest单元测试框架生成HTML测试报告及优化的步骤

    2021-07-10 16:49:13
  • golang简单位运算示例

    2024-02-08 18:53:48
  • mysql Load Data InFile 的用法

    2009-09-06 12:08:00
  • Python的iOS自动化打包实例代码

    2022-04-18 01:15:19
  • Python实现向好友发送微信消息优化篇

    2022-02-18 18:07:08
  • pytorch 液态算法实现瘦脸效果

    2021-12-05 19:28:17
  • 原生js封装的一些jquery方法(详解)

    2024-04-19 09:47:01
  • Python decimal模块的使用示例详解

    2023-02-13 15:49:52
  • 使用 Python 在京东上抢口罩的思路详解

    2023-06-01 01:10:30
  • PHP5中新增stdClass 内部保留类

    2024-05-09 14:47:55
  • mac系统OS X10.10版本安装最新5.7.9mysql的方法

    2024-01-14 10:05:38
  • asp之家 网络编程 m.aspxhome.com