2016-03-06 83 views
0

我目前在項目中使用Select2,並希望向我的選擇列表中添加一個選項,無論用戶輸入或搜索什麼內容,都會顯示該選項。這個想法是在列表中總是有一個「添加新的」選項。 我不認爲我的代碼在這裏是必要的(但如果需要的話,我可能會提供),因爲我在這個特定主題中缺乏知識的唯一方法是如何關注總是顯示的選項。 我想過使用matcher屬性,但我不知道如何。「select2」添加常量選項

回答

0

我已經設法設置一個新的匹配器,問題是我不確定如何創建一個新的匹配器,仍然使用select2默認的匹配器。 我缺少的其他東西是select2的完整版本。

function newMatcher(term, text){ 
 
    //The "ADD NEW" String is the text in the option I want to always show up. 
 
    //The code after OR looks for the actual results for the user's search 
 
    if ((text.toUpperCase().indexOf("ADD NEW") > -1) 
 
     || (text.toUpperCase().indexOf(term.toUpperCase()) > -1)) { 
 
     return true; 
 
    } 
 
} 
 
$(document).ready(function() { 
 
    $.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) { 
 
     $('select').select2({ 
 
      matcher: oldMatcher(newMatcher) 
 
     }) 
 
    }) 
 
});