限制jQuery自动完成中的结果

Mar*_*tin 10 jquery autocomplete

如何设置jQuery自动完成结果的限制?

这是我的代码:

        $.ajax({
            url: "/cache/search/SearchModels.xml",
            dataType: "xml",
            success: function(xmlResponse) {
                var data = $("SearchModel", xmlResponse).map(function() {
                    return {
                        value: $("Name", this).text() + ", " + $("Description", this).text(),
                        id: $("No", this).text(),
                        name: $("Name", this).text(),
                        url: $("URL", this).text()
                    };
                }).get();
                $("#txtTopSearch").autocomplete({
                    source: data,
                    minLength: 2,
                    select: function(event, ui) {
                        BlockUI();
                        if (typeof (ui.item.url) != 'undefined') {
                            window.location = ui.item.url;
                        }
                        else {
                            alert('Page not found!');
                            $.unblockUI();
                        }
                    },
                    search: function(event, ui) {
                        $('#txtTopSearch').addClass('searchInProgress');
                    },
                    close: function(event, ui) {
                        $('#txtTopSearch').removeClass('searchInProgress');
                    }
                }).data("autocomplete")._renderItem = function(ul, item) {
                    return $("<li></li>")
                    .data("item.autocomplete", item)
                    .append("<a><span style='font-size:.9em; font-weight:bold;'>" + item.id + "</span><br /><span style='font-size:.8em;'>" + item.name + "</span></a>")
                    .appendTo(ul);
                };
            },
            error: function(xhr, textStatus, errorThrown) {
                alert('Error: ' + xhr.statusText);
            }
        });
Run Code Online (Sandbox Code Playgroud)

此代码返回查询中的所有结果,但我想将此限制为仅显示10个结果.在旧的自动完成版本中,有一个选项,但现在已弃用.

XML示例:

<?xml version="1.0"?>
<ArrayOfSearchModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SearchModel>
    <No>1</No>
    <Name>My product</Name>
    <Description>My description</Description>
    <Tags>blue;brown;</Tags>
    <URL>/Products/1</URL>
  </SearchModel>
</ArrayOfSearchModel>
Run Code Online (Sandbox Code Playgroud)

Gab*_*oli 11

最后更新
在理解了我之前的答案后,我限制了整个xml结果集而不是自动完成的结果

在覆盖默认_renderItem方法时,可以覆盖默认方法_renderMenu.

$.ui.autocomplete.prototype._renderMenu = function( ul, items ) {
   var self = this;
   $.each( items, function( index, item ) {
      if (index < 10) // here we define how many results to show
         {self._renderItem( ul, item );}
      });
}
Run Code Online (Sandbox Code Playgroud)

从这个jQueryUI修改了答案:我如何自定义格式化Autocomplete插件结果?谢谢你去@cheeso ..


原始答案

在你success回调使用$("SearchModel:lt(10)", xmlResponse).map(...

:lt(10)索引小于10 的获取元素.因此将返回最多10个结果.

(当然10号可能是你想要的任何东西)

:lt()http://api.jquery.com/lt-selector/上查看选择器

更新

虽然我无法理解为什么第一个答案不起作用,因为它SearchModel是一个标签,我们的目标是......我们可以在map方法中移动过滤.

success: function(xmlResponse) {
                var data = $("SearchModel", xmlResponse).map(function(index) {
                    if (index<10)
                      {
                        return {
                            value: $("Name", this).text() + ", " + $("Description", this).text(),
                            id: $("No", this).text(),
                            name: $("Name", this).text(),
                            url: $("URL", this).text()
                               };
                      }
                      else
                      { return null; }
                }).get();
Run Code Online (Sandbox Code Playgroud)

地图文件()