我下载了Select2 jquery插件并完成了所有设置 - 我在这里有一个基本的下拉列表。
<SELECT NAME="GENDER">
<OPTION VALUE="1">MALE
<OPTION VALUE="2">FEMALE
<OPTION VALUE="3">UNDEFINED
</SELECT>
我应用了插件,它可以工作 - 不是问题。
我一直在审查select2文档,而我正在尝试做的不是按性别搜索,例如输入女性和男性等等 - 我希望用户只需按1即可调出男性,2女性。
是否有人在select2中尝试过此操作?
答案 0 :(得分:19)
查看文档中的“匹配器”选项:http://ivaynberg.github.io/select2/#documentation
重写matcher函数以检查给定选项的text()和val()。未经测试的例子:
$('select').select2({
matcher: function(term, text, option) {
return text.toUpperCase().indexOf(term.toUpperCase())>=0 || option.val().toUpperCase().indexOf(term.toUpperCase())>=0;
}
});
答案 1 :(得分:3)
The parameters for the "matcher" option has changed since this has been answered。我在实施美国州下拉列表时遇到了同样的问题,每个选项都像<option value="PA">Pennsylvania</option>
,我希望用户能够拼写状态或只是输入他们的代码并且它可以工作。根据更新的文档,我修改了它:
$('select').select2({
matcher: function(params, data) {
// If there are no search terms, return all of the data
if ($.trim(params.term) === '') { return data; }
// Do not display the item if there is no 'text' property
if (typeof data.text === 'undefined') { return null; }
// `params.term` is the user's search term
// `data.id` should be checked against
// `data.text` should be checked against
var q = params.term.toLowerCase();
if (data.text.toLowerCase().indexOf(q) > -1 || data.id.toLowerCase().indexOf(q) > -1) {
return $.extend({}, data, true);
}
// Return `null` if the term should not be displayed
return null;
}
});
Here is a working CodePen demo I whipped up使用OP的选择字段和美国各州的选择字段来演示这一点。
答案 2 :(得分:0)
我接受了@Tessa的出色回答,并将其与optGroup
一起使用。
function(params, option) {
// If there are no search terms, return all of the option
var searchTerm = $.trim(params.term);
if (searchTerm === '') { return option; }
// Do not display the item if there is no 'text' property
if (typeof option.text === 'undefined') { return null; }
var searchTermLower = searchTerm.toLowerCase(); // `params.term` is the user's search term
// `option.id` should be checked against
// `option.text` should be checked against
var searchFunction = function(thisOption, searchTerm) {
return thisOption.text.toLowerCase().indexOf(searchTerm) > -1 ||
(thisOption.id && thisOption.id.toLowerCase().indexOf(searchTerm) > -1);
};
if (!option.children) {
//we only need to check this option
return searchFunction(option, searchTermLower) ? option : null;
}
//need to search all the children
option.children = option
.children
.filter(function (childOption) {
return searchFunction(childOption, searchTermLower);
});
return option;
}
答案 3 :(得分:0)
$('select').select2({
matcher: function(params, data) {
// Do not display the item if there is no 'text' property
if (typeof data.text === 'undefined')
{ return null; }
// If there are no search terms, return all of the data
if (params.term === '' || typeof params.term === 'undefined') {
return data;
} else {
// `params.term` is the user's search term
// `data.id` should be checked against
// `data.text` should be checked against
// console.log(params.term.toLowerCase());
var q = params.term.toLowerCase();
if (data.text.toLowerCase().indexOf(q) > -1 || data.id.toLowerCase().indexOf(q) > -1) {
return $.extend({}, data, true);
}
}
// Return `null` if the term should not be displayed
return null;
}
});