Vue标签输入框

Vue组件、使用方法都在代码里了。效果如下:

image.png

附上所有代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.bootcss.com/vue/2.5.13/vue.min.js"></script>
    <link href="https://cdn.bootcss.com/bulma/0.6.1/css/bulma.min.css" rel="stylesheet">
    <style>
    .tags-input > ul {
        height: 16rem;
        overflow-y: scroll;
        border: 1px solid #ccc;
    }
    .tags-input li {
        padding: .25rem 1rem;
    }
    .tags-input li:hover {
        color: white;
        background: blue;
    }
    </style>
    <title>Document</title>
</head>
<body>
<div class="section" id="app">
    <tag-input :tags="tags" max="3"></tag-input>
</div>
<script>
Vue.component('tag-input', Vue.extend({
    props: ['tags', 'max'],
    template: '<div class="tags-input"><div class="input field is-grouped" v-on:click="popup=true" style="margin-bottom:0"><div class="control" v-for="(item,i) in selected" v-on:click.stop=""><div class="tags has-addons is-medium"><span class="tag is-link">{{ item }}</span><a v-on:click="move(item,false);" class="tag is-delete"></a></div></div></div><ul class="is-hoverable" v-show="popup"><li v-for="tag in tags" v-on:click="move(tag,true);popup=false;">{{ tag }}</li></ul></div>"tag in tags" v-on:click="move(tag,true);popup=false;">{{ tag }}</li></ul></div>',
    data: function () {
        return {
            popup: false,
            selected: [],
        };
    },
    methods: {
        move: function(item, flag) {
            var from = flag ? this.tags : this.selected;
            var to = flag ? this.selected : this.tags;
            if (!flag || to.length < this.max) {
                from.splice(from.indexOf(item), 1);
                to.push(item);
            }
        },
    }
}));
new Vue({
    el: '#app',
    data: {
        tags: [
            '标签1',
            '标签2',
            '标签3',
            '标签4',
            '标签5',
            '标签6',
            '标签7',
            '标签8',
            '标签9',
            '标签10',
            '标签11',
            '标签12',
        ]
    }
});
</script>
</body>
</html>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值