Select2也搜索数据属性

时间:2020-05-07 15:29:17

标签: jquery-select2 custom-data-attribute jquery-select2-4

我正在使用Select2的最新版本4.0.12。

我的下拉列表如下:

<select id="langselect">
    <option data-lang="Английский">English</option>
    <option data-lang="Русский">Russian</option>
</select>

我使用matcher选项按首字母搜索:

function matchStart(term, text) {
    if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
        return true;
    }
    return false;
}
$.fn.select2.amd.require(['select2/compat/matcher'], function(oldMatcher) {
    $("#langselect").select2({
        matcher: oldMatcher(matchStart),
    });
});

我还需要更改matchStart()函数以也开始在data-*属性中进行搜索。我正在尝试这样的事情:

if ($(data.element).data('lang').toUpperCase().indexOf(term.toUpperCase()) == 0) {
    return true;
}

但是我遇到了Cannot read property 'indexOf' of undefined错误。

请帮助解决此错误。

0 个答案:

没有答案