我正在使用 Kendo UI DropDownList 元素进行过滤搜索。
如果用户在下拉列表中搜索并且搜索到的项目不可用,我将显示+ Add
链接...
只有当我有一个<select>
框
感谢@Jonathan
,他帮助了上述内容:)
但如果我有超过1个选择框
,则会出现问题的jQuery
$(document).ready(function() {
// set up the delay function
var delay = (function(){
var timer = 0;
return function(callback, ms) {
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$(".selectBox").kendoDropDownList({
filter: "contains"
});
// set up the event handler
$(".k-list-filter input").keyup(function () {
// wait for Kendo to catch up
delay(function () {
// check the number of items in the list and make sure we don't already have an add link
if ($('.k-list-scroller ul > li').length === 0 && !($(".newItem").length)) {
$('.k-list-filter .k-i-search').hide();
$('.k-list-filter').append('<a href="javascript:;" class="newItem">+ Add</a>');
}
// check the number of items in the list
if ($('.k-list-scroller ul > li').length > 0) {
$('.k-list-filter .k-i-search').show();
$('.k-list-filter .newItem').remove();
}
}, 500); // 500 ms delay before running code
});
});
HTML
<div class="row">
<div class="col-xs-4">
<div class="field">
<select class="selectBox">
<option>-- Select 1 --</option>
<option>Lorem</option>
<option>Ipsum</option>
<option>Dolar</option>
</select>
</div>
</div>
<div class="col-xs-4">
<div class="field">
<select class="selectBox">
<option>-- Select 2 --</option>
<option>Lorem</option>
<option>Ipsum</option>
<option>Dolar</option>
<option>Sit amet lieu</option>
</select>
</div>
</div>
<div class="col-xs-4">
<div class="field">
<select class="selectBox">
<option>-- Select 3 --</option>
<option>Lorem</option>
<option>Ipsum</option>
<option>Dolar</option>
</select>
</div>
</div>
</div>
答案 0 :(得分:1)
因为.k-list-scroller
和.k-list-filter
是针对所有3个下拉列表呈现的,但如果您只需要访问当前下拉列表中的这些元素,请在this
事件中使用keyup
关键字:
$(".k-list-filter input").keyup(function () {
//"this" represents html input element
var listFilter = $(this).closest('.k-list-filter');
var listScroller = $(this).closest('.k-list-container').find('.k-list-scroller');
// wait for Kendo to catch up
delay(function () {
// check the number of items in the list and make sure we don't already have an add link
if (listScroller.find('ul > li').length === 0 && !listFilter.find(".newItem").length) {
listFilter.find('.k-i-search').hide();
listFilter.append('<a href="javascript:;" class="newItem">+ Add</a>');
}
// check the number of items in the list
if (listScroller.find('ul > li').length > 0) {
listFilter.find('.k-i-search').show();
listFilter.find('.newItem').remove();
}
}, 500); // 500 ms delay before running code
});
答案 1 :(得分:1)
为什么你想要实现的目标没有发生,有几个原因。一切都与您在keyup
函数中选择项目的方式有关。我会尽力解释原因:
您正在使用k-list-scroller
选择所有元素...其中有3个元素。所以这个表达式的结果
$('.k-list-scroller ul > li').length === 0
在给定的上下文中始终为false
这里也发生了同样的事情......
$('.k-list-filter .k-i-search').hide();
当您尝试隐藏放大镜图标
时这是一个有效的代码片段......
$(document).ready(function() {
// set up the delay function
var delay = (function(){
var timer = 0;
return function(callback, ms) {
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$(".selectBox").kendoDropDownList({
filter: "contains"
});
// set up the event handler
$(".k-list-filter input").keyup(function () {
var self = this;
// wait for Kendo to catch up
delay(function () {
// check the number of items in the list and make sure we don't already have an add link
var itemsFound = $(self).parents('.k-list-container').find(".k-list-scroller ul > li").length;
if (itemsFound === 0 && !($(".newItem").length)) {
console.log("Adding new");
setTimeout(function(){
$(self).parents('.k-list-container').find('.k-list-filter .k-i-search').hide();
$(self).parents('.k-list-container').find('.k-list-filter').append('<a href="javascript:;" class="newItem">+ Add</a>');
}, 50);
}
// check the number of items in the list
if ($('.k-list-scroller ul > li').length > 0) {
$('.k-list-filter .k-i-search').show();
$('.k-list-filter .newItem').remove();
}
}, 500); // 500 ms delay before running code
});
});
&#13;
body{font-family:Verdana;font-size:12px;margin:50px 10px;}
.k-header{border:1px solid #ccc;background:#fff;}
.newItem{position:absolute;top:8px;right:10px;}
&#13;
<link rel="stylesheet" type="text/css" href="//kendo.cdn.telerik.com/2016.1.112/styles/kendo.material.min.css"/>
<link rel="stylesheet" type="text/css" href="//kendo.cdn.telerik.com/2016.1.112/styles/kendo.common-material.min.css"/>
<link rel="stylesheet" type="text/css" href="//kendo.cdn.telerik.com/2016.1.112/styles/kendo.material.min.css"/>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="//kendo.cdn.telerik.com/2016.1.112/js/kendo.all.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-xs-4">
<div class="field">
<select class="selectBox">
<option>-- Select 1 --</option>
<option>Lorem</option>
<option>Ipsum</option>
<option>Dolar</option>
</select>
</div>
</div>
<div class="col-xs-4">
<div class="field">
<select class="selectBox">
<option>-- Select 2 --</option>
<option>Lorem</option>
<option>Ipsum</option>
<option>Dolar</option>
<option>Sit amet lieu</option>
</select>
</div>
</div>
<div class="col-xs-4">
<div class="field">
<select class="selectBox">
<option>-- Select 3 --</option>
<option>Lorem</option>
<option>Ipsum</option>
<option>Dolar</option>
</select>
</div>
</div>
</div>
&#13;