jquery自动完成限制结果

时间:2012-01-19 05:03:58

标签: jquery-ui autocomplete jquery-ui-autocomplete

我需要在自动完成时将行数限制为10。数据来自db。有没有像'maxrows'这样的属性?我不想添加滚动。

请提前感谢方法。

我的代码是:

$("#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);
                }

                });  

2 个答案:

答案 0 :(得分:4)

最简单的方法是限制源中返回结果的数量。

因此,在getiId.html中,将项目数限制为10

答案 1 :(得分:1)

您可以随时在success函数中的10个结果后停止循环:

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);
},
相关问题