【问题标题】:Select2 template does not autocomplete when tagging标记时 Select2 模板不会自动完成
【发布时间】:2015-12-24 13:48:50
【问题描述】:

我正在尝试使用 Select2.js 4.0 版来创建标签选择。如果它不退出,我希望创建一个新标签,并且我希望使用'data-html' 标签格式化结果。

问题在于这两个元素(createTagtemplateResult)独立工作,但是当我将它们一起使用时,当用户在搜索框中键入时,自动完成选项不再显示。

是我做错了什么,还是 Select2 的限制?

application.js

  $("#taglist").select2({
    tokenSeparators: [',', ' '],
    tags: true,
    createTag: function (tag) {
        // Case insensitive search for tag
        tagExists = false;
        $("#taglist option").each(function() {
            if ($.trim(tag.term).toUpperCase() == $.trim($(this).text()).toUpperCase()) {
                tagExists = true;
            }
        });

        // If the tag does not exist create it
       if (!tagExists) {
             return {
                id: tag.term,
                text: tag.term + " (create)",
                isNew: true
            };
        }
    },
    templateResult: function (choice) {
      if (!choice.id) { return choice.text; }
      var $choice = $(
        '<span><a>' + data.element.getAttribute('data-html') + '</a></span>' 
      );
      return $choice;
    }
  });

【问题讨论】:

    标签: javascript jquery html jquery-select2 jquery-select2-4


    【解决方案1】:

    标记时没有data.element,因此您的模板方法需要能够处理它。理想情况下,您的方法应该能够与 data.text 一起使用,其他任何内容都是可选的。

    因此,如果元素的 data-html 属性中没有任何内容,您应该回退到 data.text

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 2011-02-09
      • 2015-08-20
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多