手写一个类似 element-ui 的可输入下拉选择

4 篇文章 0 订阅

效果图如下:

当 鼠标 点击 输入框时,显示下拉滚动列表,点击后采用赋值给 输入框; 在输入框中输入值可以再我们的下拉列表数据中进行模糊匹配

在这里插入图片描述

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手写一个select</title>

    <div id ="app">
        <input type="text"  class="my_input" v-model.trim="inputValue" @click="openSelect">
        <div class="my_select" v-show="showMySelect">
            <ul>
                <li v-for="(item, index) in datas" :key= index>
                    <div class="select_item" @click="getClickItem(index)" v-show="canShowItem(item.value)">
                        {{item.value}}
                    </div>
                </li>
            </ul>
        </div>
    </div>
    
</head>
<body>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
               datas: [{
                   value: '蒜薹炒肉'
               },{
                   value: '莴笋炒肉'
               },{
                   value: '笋子炒肉'
               },{
                   value: '红烧牛腩'
               },{
                   value: '干锅兔'
               },{
                   value: '番茄炒鸡蛋'
               },{
                   value: '红烧牛腩'
               },{
                   value: '干煸鸡'
               },{
                   value: '红油抄手'
               },{
                   value: '蒸饺'
               }],
               inputValue: null,
               showMySelect: false
            },
         
            methods: {
                getClickItem(index) {
                    this.inputValue = this.datas[index].value;
                    this.showMySelect = false;
                },

                openSelect() {
                    this.showMySelect = true;
                },

                submit() {
                    console.log(this.inputValue)
                }
            },

            computed: {
                canShowItem() {
                    return (value) => {
                        if (this.inputValue != null && this.inputValue.length > 0 ) {
                            return value.indexOf(this.inputValue) >= 0;
                        } else {
                            return true;
                        }  
                    }
                }
            }
        })
    </script>

    <style scoped >
   

        ul {
            list-style: none;
            padding-inline-start: 0;
            max-height: 200px;
            overflow-y: scroll;
        }

        .my_input {
           width: 100%;
        }

        .my_select {
            background-color: rgb(211, 211, 211);
            width: inherit;
            text-align: center;
        }

        .select_item {
            height: 32px;
        }

        #app {
            width: 400px;
            height: 400px;
        }
    </style>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值