在选择二标签textarea的创建新的标签 [英] Creating new tags in a Select2 tag textarea

查看:122
本文介绍了在选择二标签textarea的创建新的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用了选择二的标签输入(文本区域)。因此,当在存在于我的数据的基础上的项的名称的用户的类型,它示出了匹配的项目的列表,并且用户可以选择一个与一个标签创建

I have an input (textarea) that has Select2's tags applied to it. So when a user types in the name of an item that exists in my data base, it shows a list of matching items and the user can select one and a tag is created.

下面是我的code,到目前为止基本标记功能:

Here is my code so far for basic tag functionality:

$('#usualSuppliers').select2({
        placeholder: "Usual suppliers...",
        minimumInputLength: 1,
        multiple: true,
        id: function(e) {
            return e.id + ":" + e.name;
        },
        ajax: {
            url: ROOT + 'Ajax',
            dataType: 'json',
            type: 'POST',
            data: function(term, page) {

                return {
                    call: 'Record->supplierHelper',
                    q: term,
                    page_limit: 10
                };
            },
            results: function(data, page) {
                return {
                    results: data.suppliers
                };
            }
        },
        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],
                    title: item[1]
                });
            });
            //$(element).val('');
            callback(data);
        }
    });

有没有,如果键入的文本不存在要创建一个新的标签的方法吗?起初我以为这可能一些如何用空格分隔来完成,但是某些项目(供应商名称)将会有空格他们,所以这是行不通的。

Is there a way for a new tag to be created if the text typed does not exist? Initially I thought this could some how be done by delimiting with spaces, but some items (supplier names) will have spaces in them, so that won't work.

我认为,当没有找到匹配的用户需要以某种方式创造由$ P $标签pssing可能出现在下拉框中选择一个按钮,但我不知道如何做到这一点。

I think when no matches are found the user needs to somehow "create" the tag by pressing a button that could appear in the drop down box, but I have no idea how to do this.

我怎么能允许用户创建可以有空格他们仍然能够继续添加更多标签新的标签,现有的或以其他方式?

How can I allow users to create new tags that may have spaces in them and still be able to carry on adding more tags, existing or otherwise?

推荐答案

是的,你可以做到这一点。还有就是文档中的一个例子。看看 http://ivaynberg.github.io/select2/#events

Yes you can do it. There is a example in the documentation. Look at http://ivaynberg.github.io/select2/#events

$("#e11_2").select2({
  createSearchChoice: function(term, data) { 

   if ($(data).filter( function() { return this.text.localeCompare(term)===0;   
         }).length===0) {
     return {id:term, text:term};
   } 

  },
  multiple: true,
  data: [{id: 0, text: 'story'},{id: 1, text: 'bug'},{id: 2, text: 'task'}]
});

您必须创建一个像createSearchChoice一个函数,该函数返回一个对象,ID和文本。在其他情况下,如果返回undefined选择不将被创建。

You have to create a function like createSearchChoice, that returns a object with 'id' and 'text'. In other case, if you return undefined the option not will be created.

这篇关于在选择二标签textarea的创建新的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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