Select2限制标签数量 [英] Select2 limit number of tags

查看:775
本文介绍了Select2限制标签数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法限制用户可以使用Select2添加到输入字段的标签数量?

Is there a way to limit the number of tags a user can add to an input field using Select2?

我有:

$('#tags').select2({
    containerCssClass: 'supplierTags',
    placeholder: "Usual suppliers...",
    minimumInputLength: 2,
    multiple: true,
    tokenSeparators: [",", " "],
    placeholder: 'Usual suppliers...',
            createSearchChoice: function(term, data) {
                if ($(data).filter(function() {
                    return this.name.localeCompare(term) === 0;
                }).length === 0) {
                    return {id: 0, name: term};
                }

            },
    id: function(e) {
        return e.id + ":" + e.name;
    },
    ajax: {
        url: ROOT + 'Call',
        dataType: 'json',
        type: 'POST',
        data: function(term, page) {
            return {
                call: 'Helpers->tagsHelper',
                q: term
            };
        },
        results: function(data, page) {
            return {
                results: data.tags
            };
        }
    },
    formatResult: formatResult,
    formatSelection: formatSelection,
    initSelection: function(element, callback) {
        var data = [];
        $(element.val().split(",")).each(function(i) {
            var item = this.split(':');
            data.push({
                id: item[0],
                name: item[1]
            });
        });
        callback(data);
    }
});

如果可能有/是一个简单的参数,如,那就太棒了:5 并在达到限制时触发回调。

It would be great if there could be/is a simple parameter like limit: 5 and a callback to fire when the limit is reached.

推荐答案

当然,用 maximumSelectionLength 如下:

$("#tags").select2({
    maximumSelectionLength: 3
});




最大选择长度

Select2允许开发者限制在多选控件中选择
的商品数量。

Select2 allows the developer to limit the number of items that can be selected in a multi-select control.

http://ivaynberg.github.io/select2/

它没有本机回调,但您可以将函数传递给 formatSelectionTooBig ,如下所示:

It has no native callback, but you can pass a function to formatSelectionTooBig like this:

$(function () {
    $("#tags").select2({
        maximumSelectionLength: 3,
        formatSelectionTooBig: function (limit) {

            // Callback

            return 'Too many selected items';
        }
    });
});

http:// jsfiddle.net/U98V7/

或者您可以像这样扩展 formatSelectionTooBig

Or you could extend formatSelectionTooBig like this:

$(function () {
    $.extend($.fn.select2.defaults, {
        formatSelectionTooBig: function (limit) {

            // Callback

            return 'Too many selected items';
        }
    });

    $("#tags").select2({
        maximumSelectionLength: 3
    });
});

修改

使用更新的 maximumSelectionLength 替换 maximumSelectionSize 。谢谢@DrewKennedy!

Replaced maximumSelectionSize with the updated maximumSelectionLength. Thanks @DrewKennedy!

这篇关于Select2限制标签数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆