Kendo UI [DropDownList] - 多个元素的冲突

时间:2016-02-24 07:33:44

标签: javascript jquery kendo-ui

我正在使用 Kendo UI DropDownList 元素进行过滤搜索。

如果用户在下拉列表中搜索并且搜索到的项目不可用,我将显示+ Add链接...

只有当我有一个<select>

时,这才能正常工作

感谢@Jonathan,他帮助了上述内容:)

但如果我有超过1个选择框

,则会出现问题
  

Online Demo

的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>

2 个答案:

答案 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函数中选择项目的方式有关。我会尽力解释原因:

  1. 您正在使用k-list-scroller选择所有元素...其中有3个元素。所以这个表达式的结果

    $('.k-list-scroller ul > li').length === 0

  2. 在给定的上下文中始终为false

    1. 这里也发生了同样的事情......

      $('.k-list-filter .k-i-search').hide();

    2. 当您尝试隐藏放大镜图标

      1. 最后但并非最不重要的是,当满足上述条件时,您需要延迟执行代码块,因为kendo / telerik操纵下拉项并使其可见,换句话说,只要您隐藏搜索图标telerik将立即显示它。
      2. 这是一个有效的代码片段......

        &#13;
        &#13;
        $(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;
        &#13;
        &#13;