jquery autocomplete limit results

I need to limit the row numbers with 10 while autocomplete. Data is from db. Is there any attribute like 'maxrows' or something?. I don't want to add the scroll.

please suggest method thanks in advance.

my code is:

$("#iIdlbl").autocomplete({
                    source : function(request, response) {      

                    var value = jQuery("#iIdlbl").val();


                    $.ajax( {
                        url : 'getiId.html',
                        dataType : 'json',
                        data : {
                            filter : value
                        },
                        success : function(data) {

                            response(jQuery.map(data.iList,function(item) {
                                                return {
                                                    value : item.iId,
                                                    key : item.iId

                                                };
                                            }));


                },
                error : function(XMLHttpRequest,textStatus,errorThrown) {
                    $.loader('close');
                }
                    });

                },

                minLength : 0,
                open : function() {
                    $(this).removeClass(
                            "ui-corner-all").addClass(
                            "ui-corner-top");
                },

                close : function() {
                    $(this).removeClass(
                            "ui-corner-top").addClass(
                            "ui-corner-all");
                },
                select : function(event, ui) {
                    searchById(ui.item.value);
                }

                });  

Upvotes: 3

Views: 4186

Answers (2)

Andrew Whitaker
Andrew Whitaker

Reputation: 126052

You could always just stop looping after 10 results in your success function:

success : function(data) {
    var results = [], i, 
        length = 10 < data.iList.length ? 10 : data.iList.length;

    for (i = 0; i < length; i++) {
        results.push({
           value: item.iId,
           key: item.iId
        });
    }
    response(results);
},

Upvotes: 1

joe_coolish
joe_coolish

Reputation: 7259

The easiest way is to limit the number of returned results in your source.

so, in getiId.html, limit the number of items to 10

Upvotes: 4

Related Questions