Limit the result in jQuery Autocomplete

前端 未结 5 1536
情书的邮戳
情书的邮戳 2021-01-04 18:45

How can you set a limit on the result from the jQuery autocomplete?

This is my code:

        $.ajax({
            url: \"/cache/search/SearchModels.x         


        
相关标签:
5条回答
  • 2021-01-04 19:17

    Why not limit the data that your query returns from your xml source?

    Edit:

    You have to remember that the autocomplete functionality is essentially using a regex to match items in the datasource. Having a large datasource is bad because it has to parse so much data simply to find the correct item.

    0 讨论(0)
  • 2021-01-04 19:22

    You could add a handler for the "open" event:

        open:function(event,ui){ 
        var maxListLength=10;
        var ul = jQuery( "#txtTopSearch" ).autocomplete("widget")[0];
        while(ul.childNodes.length > maxListLength)
              ul.removeChild(ul.childNodes[ul.childNodes.length-1]);
        }
    
    0 讨论(0)
  • 2021-01-04 19:28

    This is what I did based on some reading of the autocomplete API docs

    $input.autocomplete({
      source: function( request, response ) {
        $.getJSON('search.php', request, function( data, status, xhr ) {
          // return a max of 10 results.
          response( data.slice(0, 9) );
        });
      }
    })
    

    Following this pattern saves having to do any funky stuff in the render layer.

    0 讨论(0)
  • 2021-01-04 19:32

    The quickest way to limit results is doing it during the "open" event. We can delete part of the content dynamically created by jquery ui, reducing the children element array.

    This solution solved the same issue for me:

    var maxResults = 10;
    $input.autocomplete({
        source: somefile.json,
        open: function(event,ui){
              $(this).data("uiAutocomplete").menu.element.children().slice(maxResults).remove();
             }
    })
    
    0 讨论(0)
  • 2021-01-04 19:39

    Final Update
    after understanding that in my previous answers i was limiting the whole xml result set and not the results of the autocomplete

    As you have overridden the default _renderItem method, you can override the default _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 );}
          });
    }
    

    answer is modified from this jQueryUI: how can I custom-format the Autocomplete plug-in results? so thanks go to @cheeso..


    Original Answer

    In you success callback use $("SearchModel:lt(10)", xmlResponse).map(...

    The :lt(10) gets elements with an index of less than 10. So max 10 results will be returned..

    (of course the number 10 could be anything you want)

    Look at :lt() selector at http://api.jquery.com/lt-selector/

    update

    Although i cannot understand why the first answer does not work, since the SearchModel is a tag and we target that.. we can move the filtering in the map method..

    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();
    

    documentation of map()

    0 讨论(0)
提交回复
热议问题