我使用select2与Bootstrap 3. 现在我想知道是否可以显示所有optgroup项目,如果搜索匹配optgroup名称,同时仍然能够搜索项目为好。如果这是可能的,我该怎么做?显示结果匹配optgroup使用select2
6
A
回答
18
居然发现解决方案通过修改匹配选择
$("#myselect").select2({
matcher: function(term, text, opt){
return text.toUpperCase().indexOf(term.toUpperCase())>=0 || opt.parent("optgroup").attr("label").toUpperCase().indexOf(term.toUpperCase())>=0
}
});
的前提下,该标签属性已在各OPTGROUP设置。
0
的人一些小的改动建议代码,少重复,当没有父optgroups科佩斯:
$('select').select2({
matcher: function(term, text, opt){
var matcher = opt.parent('select').select2.defaults.matcher;
return matcher(term, text) || (opt.parent('optgroup').length && matcher(term, opt.parent('optgroup').attr("label")));
}
});
10
以上答案似乎不选择二4.0,所以如果你的工作开箱“再狩猎的是,检查了这一点:https://github.com/select2/select2/issues/3034
(使用这样的功能:$("#example").select2({matcher: modelMatcher});
)
function modelMatcher (params, data) {
data.parentText = data.parentText || "";
// Always return the object if there is nothing to compare
if ($.trim(params.term) === '') {
return data;
}
// Do a recursive check for options with children
if (data.children && data.children.length > 0) {
// Clone the data object if there are children
// This is required as we modify the object to remove any non-matches
var match = $.extend(true, {}, data);
// Check each child of the option
for (var c = data.children.length - 1; c >= 0; c--) {
var child = data.children[c];
child.parentText += data.parentText + " " + data.text;
var matches = modelMatcher(params, child);
// If there wasn't a match, remove the object in the array
if (matches == null) {
match.children.splice(c, 1);
}
}
// If any children matched, return the new object
if (match.children.length > 0) {
return match;
}
// If there were no matching children, check just the plain object
return modelMatcher(params, match);
}
// If the typed-in term matches the text of this term, or the text from any
// parent term, then it's a match.
var original = (data.parentText + ' ' + data.text).toUpperCase();
var term = params.term.toUpperCase();
// Check if the text contains the term
if (original.indexOf(term) > -1) {
return data;
}
// If it doesn't contain the term, don't return anything
return null;
}
+0
这工作。这现在应该是答案,因为上述确实有问题。感谢willbradley。 – bubjavier
相关问题
- 1. Select2显示所有结果
- 2. optgroup中的Select2 optgroup
- 3. 记忆游戏显示结果匹配
- 4. 匹配表数据以显示结果
- 5. 如何覆盖.select2结果.select2 - 突出显示的颜色
- 6. 如何使用ajax在select2中显示多个结果数据?
- 7. 筛选结果显示与NG-显示角度JS匹配结果
- 8. 精确匹配选项,OPTGROUP
- 9. 连接表的显示结果,即使某些值不匹配
- 10. 如果没有结果匹配键,则显示所有项目
- 11. 如果任何组合匹配,MySQL会显示结果
- 12. 使用foreach显示结果
- 13. PHP SQL序列号之间,如果匹配显示OK,如果不匹配,显示不匹配
- 14. 使用模式匹配net.liftweb.util.JSONParser.parse的结果
- 15. Microsoft Excel使用匹配结果
- 16. 使用JOIN匹配第一个结果
- 17. 匹配结构MGO结果
- 18. Git扩展显示与搜索过滤器不匹配的结果以及匹配结果
- 19. SELECT2空的结果
- 20. 遍历两个数组并比较结果以显示不匹配的结果
- 21. mysql查询匹配多个单词和显示结果使用php
- 22. Select2显示undefined
- 23. OpenGrok结果未显示匹配的行内容
- 24. Java正则表达式匹配器显示意外的结果
- 25. 匹配结果并在空格前显示文本
- 26. 查询匹配多个关键字并显示结果?
- 27. 同位素 - 限制显示匹配结果数
- 28. 匹配日期时间不显示结果
- 29. 如何显示非匹配的MySQL结果
- 30. 比较两个文件并显示匹配结果
不错!谢谢。 – flu
谢谢,那真是超级有用! – Dave
不错!不过,我建议使用默认的匹配器实现,除非你故意偏离默认的匹配器。 'element.select2({matcher:function(term,text,opt){return element.select2.defaults.matcher(term,text)|| element.select2.defaults.matcher(term,opt.parent(“optgroup”)) .attr(“label”));}});'这可以通过不仅忽略大小写而且忽略变音符号来实现更好的匹配。 – hvd