2014-10-12 81 views
0

我在使用select-2与我的Rails应用程序自动填充Ajax调用字段并允许多个值。如何在找不到匹配项时使用Select2动态添加新项目?

我CoffeScript文件:

$(document).ready -> 
    $('.select2').each (i, e) => 
    select = $(e) 
    options = 
     placeholder: select.data('placeholder') 
     multiple: true 
     width: "100%"  
     maximumSelectionSize: 20 
     tokenSeparators: [",", " "] 
     dropdownClass: 'bigdrop'  

    if select.hasClass('ajax') 
     options.ajax = 
     url: select.data('source') 
     dataType: 'json' 
     data: (term, page) -> 
      q: term 
      page: page 
      per: 25 
     results: (data, page) -> 
      results: data.resources 
      more: data.total > (page * 25) 

     options.dropdownCssClass = "bigdrop" 

    select.select2(options) 

我希望能够到的情况下,增加新的项目没有从服务器的结果,目前它不会让我增加新项目(不保存在服务器并通过Ajax调用进行填充)。

回答

0

我不得不使用createSearchChoice类似以下内容:

createSearchChoice: (term, data) -> 
    if $(data).filter(-> 
     this.text.localeCompare(term) is 0 
    ).length is 0 
     id: term 
     text: term 
相关问题