2014-10-12 90 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 
相關問題