【问题标题】:jquery autocomplete limit resultsjquery自动完成限制结果
【发布时间】:2012-01-19 21:29:06
【问题描述】:

我需要在自动完成时将行数限制为 10。数据来自分贝。有没有像'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);
                }

                });  

【问题讨论】:

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


    【解决方案1】:

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

    所以,在getiId.html 中,将项目数限制为 10

    【讨论】:

    • 好的。如果可行,请记住接受我的回答。我注意到你的接受率为 0%。 +1 总是很棒的 :)
    • 再次感谢我使用了这种方法。通过限制查询的计数..还有其他方法吗? maxItemsToShow: 10 不工作
    【解决方案2】:

    您总是可以在 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);
    },
    

    【讨论】:

    • @Andrew Whitaker this is not working error got like 'results undefined'
    • @Kandhi:对不起,我打错了。
    • @Kandhi:这不是这个小部件的有效选项。您需要自己实现此功能。
    猜你喜欢
    • 2011-05-03
    • 2011-11-28
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2018-07-24
    • 2012-07-16
    • 2011-01-04
    相关资源
    最近更新 更多