[英]jquery-chosen dropdown should get matched search results at the top
I have jquery chosen dropdown that works fine for my need.我有 jquery 选择的下拉菜单,可以很好地满足我的需要。 The only requirement I have is can we get the matched search results (options that starts with search key) at the top.我唯一的要求是我们可以在顶部获得匹配的搜索结果(以搜索键开头的选项)。
If I have 3 options Foo
, Boo
, ooo
then search for oo
.如果我有 3 个选项Foo
, Boo
, ooo
然后搜索oo
。 Getting the results in alphabetical order like Boo
, Foo
, ooo
.按字母顺序获取结果,如Boo
、 Foo
、 ooo
。 But, can we get a result like ooo
, Boo
, Foo
.但是,我们能得到像ooo
, Boo
, Foo
这样的结果ooo
?
$('.chosen-select').chosen({
width: "100%",
search_contains: true
});
I'm not sure if the plugin has any support for this.我不确定该插件是否对此有任何支持。 Please suggest.请建议。
You can write your custom logic like below.您可以像下面这样编写自定义逻辑。 I have added fiddle for same in comments.我在评论中添加了相同的小提琴。
<div id = "results"></div>
$( document ).ready(function() {
var chosenValues = ["Foo", "Boo", "Ooo"];
var sortedData = checkStartsWith(chosenValues, 'oo');
$("#results").text(sortedData)
});
function checkStartsWith(chosenValues, searchTerm) {
var matchedData = [];
var unMatchedData = [];
for (var i = 0; i < chosenValues.length; i++) {
// This condition will check if searchTerm is at the beginning of data
if (chosenValues[i].toLowerCase().indexOf(searchTerm.toLowerCase()) == 0) {
matchedData.push(chosenValues[i]);
}
else {
unMatchedData.push(chosenValues[i]);
}
}
matchedData.sort();
unMatchedData.sort();
return (matchedData.concat(unMatchedData));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.