2016-03-06 84 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) 
 
     }) 
 
    }) 
 
});