“没有找到结果”JqueryUI类别列表

时间:2013-09-21 14:21:36

标签: javascript jquery jquery-ui

我在这里遇到的问题是http://jqueryui.com/autocomplete/#categories我无法弄清楚如何在自动完成菜单搜索的下拉列表中显示“找不到结果”。有人可以在那里分享如何实现这一目标吗?

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Autocomplete - Categories</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <style>
  .ui-autocomplete-category {
    font-weight: bold;
    padding: .2em .4em;
    margin: .8em 0 .2em;
    line-height: 1.5;
  }
  </style>
  <script>
  $.widget( "custom.catcomplete", $.ui.autocomplete, {
    _renderMenu: function( ul, items ) {
      var that = this,
        currentCategory = "";
      $.each( items, function( index, item ) {
        if ( item.category != currentCategory ) {
          ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
          currentCategory = item.category;
        }
        that._renderItemData( ul, item );
      });
    }
  });
  </script>
  <script>
  $(function() {
    var data = [
      { label: "anders", category: "" },
      { label: "andreas", category: "" },
      { label: "antal", category: "" },
      { label: "annhhx10", category: "Products" },
      { label: "annk K12", category: "Products" },
      { label: "annttop C13", category: "Products" },
      { label: "anders andersson", category: "People" },
      { label: "andreas andersson", category: "People" },
      { label: "andreas johnson", category: "People" }
    ];

    $( "#search" ).catcomplete({
      delay: 0,
      source: data
    });
  });
  </script>
</head>
<body>

<label for="search">Search: </label>
<input id="search" />


</body>
</html>

1 个答案:

答案 0 :(得分:1)

  $( "#search" ).catcomplete({
     delay: 0,
     source: data
  });

替换为:

  $( "#search" ).catcomplete({
      delay: 0,
      source: function(request, response) {
         var result = data.slice(0);
         result = $.ui.autocomplete.filter(result, request.term);

        if(! result.length) {
            result.push({
              label: 'No Result Found',
              category: "",
              isPlaceholder: true
            });
        }
       response(result);
     }
  });

请参阅Example