【问题标题】:select2 remove tag_list itemselect2 删除 tag_list 项目
【发布时间】:2016-01-04 00:22:49
【问题描述】:

试图让 Select2 删除一个项目并找出它为什么在更新时不断添加新标签。

使用 Select2.JS V3.5

已关注ActiveAdmin / Select2 example

当我尝试删除一个项目时,它不会删除它。它还最终将标签列表组合到一个新的 STRING 中,从而创建一个新的标签。即如果我有 ["play", "doe"] 并且我更新了另一个字段,它会创建一个名为 "play doe" 的新标签,从而导致 ["play", "doe", "play doe"]。每次我更新时都会继续。

这是我的 JS 代码

// Generated by CoffeeScript 1.9.3
$(document).ready(function() {
 $('.tagselect').each(function() {
   var placeholder, saved, url;
   placeholder = $(this).data('placeholder');
   url = $(this).data('url');
   saved = $(this).data('saved');
   $(this).select2({
     tags: true,
     placeholder: placeholder,
     minimumInputLength: 3,
     allowClear: true,
     multiple: true,
     debug: true,
     initSelection: function(element, callback) {
       saved && callback(saved);
     },
     ajax: {
       url: url,
       dataType: 'json',
       data: function(term) {
         return {
           q: term
         };
       },
       results: function(data) {
         return {
           results: data
         };
       }
     },
     createSearchChoice: function(term, data) {
       if ($(data).filter((function() {
            return this.text.localeCompare(term) === 0;
             })).length === 0) {
            return {
              id: term,
              text: term
            };
          }
        }
      });
    });
  });

【问题讨论】:

    标签: activeadmin select2-rails select2


    【解决方案1】:

    看到https://github.com/mbleigh/acts-as-taggable-on/issues/676

    在我的模型中添加块,修复了所有标签作为扁平字符串与逗号分隔字符串出现的格式错误。现在是猴子补丁。

    class MyModel < ActiveRecord::Base
      ...
      def tag_list
        super.to_s
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多