Search code examples
jquerykendo-uikendo-dropdown

Kendo UI [DropDownList] - Filter search, show message if search item did not found


I am using Kendo UI DropDownList with search filter...

How can I replace search icon with '+ Add' link with 'No items available' message if searched item is not available in the dropdown...

Online Demo

Please refer the below image for more clarification... enter image description here

jQuery

$(document).ready(function() {

    $("#products").kendoDropDownList({
        filter: "contains",
    });

    if ($('.k-list-scroller ul').length == 0){
        $('.k-list-filter .k-i-search').hide();
        $('.k-list-filter').append('<a href="#" id="newItem">+ Add</a>');
    }

    if ($('.k-list-scroller ul').length > 0){
        $('.k-list-filter .k-i-search').show();
        $('.k-list-filter #newItem').remove();
    }

});

HTML

<h4>Products</h4>
<select id="products">
    <option>Lorem</option>
    <option>Ipsum</option>
    <option>Dolar</option>
    <option>Sit amet lieu</option>
</select>

Solution

  • You're only running your code when the page loads($(document).ready()). You need to add an event handler to use your code each time that textbox updates. I was able to add a keyup event for that.

    Once that's added, however, the code runs before kendo knows that the values in the dropdown have changed. Using a delay, we're able to hold off a moment and let the dropdown update properly.

    Note: I used 500 ms for the delay, but that number isn't the number.

    $(document).ready(function() {
      // set up the delay function
      var delay = (function(){
        var timer = 0;
        return function(callback, ms) {
          clearTimeout (timer);
          timer = setTimeout(callback, ms);
        };
      })();
        
      $("#products").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="#" id="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 { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }
    <!DOCTYPE html>
    <html>
    <head>
    <base href="http://demos.telerik.com/kendo-ui/dropdownlist/serverfiltering">
    <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.1.112/styles/kendo.common-material.min.css" />
    <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.1.112/styles/kendo.material.min.css" />
    <script src="//kendo.cdn.telerik.com/2016.1.112/js/jquery.min.js"></script>
    <script src="//kendo.cdn.telerik.com/2016.1.112/js/kendo.all.min.js"></script>
    </head>
    <body>
    
      <h4>Products</h4>
      <select id="products">
        <option>Lorem</option>
        <option>Ipsum</option>
        <option>Dolar</option>
        <option>Sit amet lieu</option>
      </select>
          
    </body>
    </html>