Vue實現(xiàn)帶有輸入篩選功能的選擇框

css

<style>
        *{
            margin:0;
            padding:0;
        }
        li{
            list-style: none;
        }
        .sel{
            width:500px;
            margin:200px auto;
            position: relative;
        }
        .sel input{
            width:496px;
            height:50px;
            outline: none;
            text-indent: 2em
        }
        .sel ul{
            width:500px;
            position: absolute;
            left:0;
            top:54px;
        }
        .sel li{
            height:50px;
            line-height: 50px;
            color:white;
            background: orange;
            margin-top:2px;
            text-indent: 2em
        }
        .icon{
            width:30px;
            height:25px;
            background: url(icon.png) no-repeat;
            background-position:0 -25px;
            position: absolute;
            right:20px;
            top:20px;
            transition:all 0.1s;
        }
        .toTop{
            background-position:0 0;
        }
    </style>

html

<div id="app">
        <div class="sel">
            <input type="text" v-model.trim="con">
            <ul v-show="flag">
                <li v-for="item in arr" @click="cl(item)">{{item}}</li>
            </ul>
            <div @click="toggle" :class="{icon:true,toTop:flag}"></div>
        </div>
</div>

js

var app=new Vue({
        el:"#app",
        data:{
            list:["蘋果","西瓜","番茄"],
            flag:false,
            con:"",
        },
        computed:{
            arr:function(){
                var arr=this.list.filter(function(item,index){
                    if(item.indexOf(this.con)!=-1){
                        return item;
                    }
                },this);
                return arr;
            }
        },
        watch:{
            con:function(val){
                if(val){
                    this.flag=true;
                }else{
                    this.flag=false;
                }
            },
            arr:function(val){
                if(val.length==0){
                    this.flag=false;
                }
            }
        },
        methods:{
            toggle:function(){
                this.flag=!this.flag;
            },
            cl:function(item){
                this.con=item;
                var self=this;
                this.$nextTick(function(){
                    self.flag=false
                })
            }
        },
        mounted:function(){
            var self=this;
            document.addEventListener("click",function(e){
                e.stopPropagation();
                var target=e.target;
                if(target.parentNode.className!="sel"||target.parentNode==null){
                    self.flag=false;
                }
            })
        }
    })

如有問題歡迎交流,微信:weiwei260104

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容