1.vue.js选择框可输入(带匹配)

选择框带输入,匹配字符
在这里插入图片描述

组件

  • template

    <template id="select-input-url">
        <label>
            <input type="text" v-model="value" class="form-control" :placeholder="'过滤-'+name"
                   @focus="focus($event)" @blur="blur($event)" :style="'width:'+width+'px'">
            <select class="together" v-model="select" id="test" v-show="show" @MouseOver="mover" @MouseOut="mout"
                    :style="'width:'+width+'px'" @click="sclick">
                <option v-for="(val,key,i) in option" :value="key" v-show="contain(val)" @MouseOver="moveroption($event)"
                        @MouseOut="moutoption($event)">[[val]]
                </option>
            </select>
        </label>
    </template>
    
  • js

    Vue.component('select-input-url', {
            delimiters: ['[[', ']]'],
            template: '#select-input-url',
            props: {
                name: {
                    type: String,
                    required: true
                },
                width: {
                    type: String,
                    default: function () {
                        return '100'
                    }
                },
                url: {
                    type: String,
                    required: true
                }
            },
            watch: {
                value: function () {
                    this.$emit('get-value', this.value)
                }
            },
            methods: {
                sclick: function () {
                    this.value = this.option[this.select];
                    this.$emit('get-key', this.select);
                    this.show = false
                },
                focus: function (e) {
                    e.currentTarget.nextElementSibling.size = 5;
                    this.show = true;
                },
                blur: function (e) {
                    if (!this.mousehover) {
                        e.currentTarget.nextElementSibling.size = 0;
                        this.show = false
                    }
                },
                mover: function () {
                    this.mousehover = true
                },
                mout: function () {
                    this.mousehover = false
                },
                contain: function (val) {
                    let v = String(val);
                    return v.indexOf(this.value) !== -1
                },
                getoption: function () {
                    let _this = this;
                    $.ajax({
                        type: "GET",
                        url: _this.url,
                        success: function (data) {
                            _this.option = data.data;
                        },
                        error: function (err) {
                            toastr.error(err.responseJSON.msg)
                        }
                    });
                },
                moveroption: function (e) {
                    e.currentTarget.style.backgroundColor = '#87CEFA';
                    e.currentTarget.style.color = '#F8F8FF'
                },
                moutoption: function (e) {
                    e.currentTarget.style.backgroundColor = '#F8F8FF';
                    e.currentTarget.style.color = '#000000'
                }
            },
            created: function () {
                this.getoption()
            },
            data(){
                return {
                    value: '',
                    select: '',
                    show: false,
                    mousehover: false,
                    option: {}
                };
            }
        });
    

使用

<select-input-url name="主机群名" width="200" @get-value="getGroup" url="/zabbix/hostgroups/?select=1"></select-input-url>

后端设置好请求路径,返回数据为对象即可。

显示值为value,选中值为key

可根据@get-value以及@get-key来获取对应值

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值