限制 jQuery UI 自动完成中的结果

发布于 2024-12-07 06:03:21 字数 192 浏览 0 评论 0原文

我正在使用 jQuery UI 自动完成。

 $("#task").autocomplete({
     max:10,
     minLength:3,
     source: myarray
 });          

max 参数不起作用,我仍然得到超过 10 个结果。我错过了什么吗?

I am using jQuery UI Autocomplete.

 $("#task").autocomplete({
     max:10,
     minLength:3,
     source: myarray
 });          

The max parameter doesn't work and I still get more than 10 results. Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

友欢 2024-12-14 06:03:21

这是 jQueryUI 小部件的正确文档。没有用于限制最大结果的内置参数,但您可以轻松完成:

$("#auto").autocomplete({
    source: function(request, response) {
        var results = $.ui.autocomplete.filter(myarray, request.term);

        response(results.slice(0, 10));
    }
});

您可以向 source 参数提供一个函数,然后调用 对过滤后的数组进行切片

这是一个工作示例: http://jsfiddle.net/andrewwhitaker/vqwBP/

Here is the proper documentation for the jQueryUI widget. There isn't a built-in parameter for limiting max results, but you can accomplish it easily:

$("#auto").autocomplete({
    source: function(request, response) {
        var results = $.ui.autocomplete.filter(myarray, request.term);

        response(results.slice(0, 10));
    }
});

You can supply a function to the source parameter and then call slice on the filtered array.

Here's a working example: http://jsfiddle.net/andrewwhitaker/vqwBP/

通知家属抬走 2024-12-14 06:03:21

您可以将 minlength 选项设置为某个大值,也可以通过这样的 css 来实现,

.ui-autocomplete { height: 200px; overflow-y: scroll; overflow-x: hidden;}

You can set the minlength option to some big value or you can do it by css like this,

.ui-autocomplete { height: 200px; overflow-y: scroll; overflow-x: hidden;}

就像“Jayantha”所说,使用 css 将是最简单的方法,但这可能会更好,

.ui-autocomplete { max-height: 200px; overflow-y: scroll; overflow-x: hidden;}

请注意,唯一的区别是“最大高度”。这将允许小部件调整到更小的高度,但不超过 200px

Same like "Jayantha" said using css would be the easiest approach, but this might be better,

.ui-autocomplete { max-height: 200px; overflow-y: scroll; overflow-x: hidden;}

Note the only difference is "max-height". this will allow the widget to resize to smaller height but not more than 200px

污味仙女 2024-12-14 06:03:21

添加到 Andrew 的答案,您甚至可以引入一个 maxResults 属性并以这种方式使用它:

$("#auto").autocomplete({ 
    maxResults: 10,
    source: function(request, response) {
        var results = $.ui.autocomplete.filter(src, request.term);
        response(results.slice(0, this.options.maxResults));
    }
});

jsFiddle: http://jsfiddle.net/vqwBP/877/

这应该有助于代码的可读性和可维护性!

Adding to Andrew's answer, you can even introduce a maxResults property and use it this way:

$("#auto").autocomplete({ 
    maxResults: 10,
    source: function(request, response) {
        var results = $.ui.autocomplete.filter(src, request.term);
        response(results.slice(0, this.options.maxResults));
    }
});

jsFiddle: http://jsfiddle.net/vqwBP/877/

This should help code readability and maintainability!

混吃等死 2024-12-14 06:03:21

这是我使用的

.ui-autocomplete { max-height: 200px; overflow-y: auto; overflow-x: hidden;}

溢出自动,因此滚动条在不应该显示时不会显示。

here is what I used

.ui-autocomplete { max-height: 200px; overflow-y: auto; overflow-x: hidden;}

The overflow auto so the scroll bar will not show when it's not supposed to.

自演自醉 2024-12-14 06:03:21

我可以通过将以下内容添加到我的 CSS 文件中来解决这个问题:

.ui-autocomplete {
    max-height: 200px;
    overflow-y: auto;
    overflow-x: hidden;
}

I could solve this problem by adding the following content to my CSS file:

.ui-autocomplete {
    max-height: 200px;
    overflow-y: auto;
    overflow-x: hidden;
}
情魔剑神 2024-12-14 06:03:21

如果结果来自 mysql 查询,则直接限制 mysql 结果会更有效:

select [...] from [...] order by [...] limit 0,10

其中 10 是您想要的最大行数

If the results come from a mysql query, it is more efficient to limit directly the mysql result:

select [...] from [...] order by [...] limit 0,10

where 10 is the max numbers of rows you want

深白境迁sunset 2024-12-14 06:03:21

我通过以下方式做到了:

success: function (result) {
response($.map(result.d.slice(0,10), function (item) {
return {
// Mapping to Required columns (Employee Name and Employee No)
label: item.EmployeeName,
value: item.EmployeeNo
}
}
));

I did it in following way :

success: function (result) {
response($.map(result.d.slice(0,10), function (item) {
return {
// Mapping to Required columns (Employee Name and Employee No)
label: item.EmployeeName,
value: item.EmployeeNo
}
}
));
屋顶上的小猫咪 2024-12-14 06:03:21

当您将自动完成附加到输入时,jQuery 允许您更改默认设置:

$('#autocomplete-form').autocomplete({
   maxHeight: 200, //you could easily change this maxHeight value
   lookup: array, //the array that has all of the autocomplete items
   onSelect: function(clicked_item){
      //whatever that has to be done when clicked on the item
   }
});

jQuery allows you to change the default settings when you are attaching autocomplete to an input:

$('#autocomplete-form').autocomplete({
   maxHeight: 200, //you could easily change this maxHeight value
   lookup: array, //the array that has all of the autocomplete items
   onSelect: function(clicked_item){
      //whatever that has to be done when clicked on the item
   }
});
青春有你 2024-12-14 06:03:21

插件:
jquery-ui-autocomplete-scroll
带有滚动条和限制结果很漂亮

$('#task').autocomplete({
  maxShowItems: 5,
  source: myarray
});

Plugin:
jquery-ui-autocomplete-scroll
with scroller and limit results are beautiful

$('#task').autocomplete({
  maxShowItems: 5,
  source: myarray
});
风和你 2024-12-14 06:03:21

我已经尝试了上面所有的解决方案,但我的只能以这种方式工作:

success: function (data) {
    response($.map(data.slice (0,10), function(item) {
    return {
    value: item.nome
    };
    }));
},

I've tried all the solutions above, but mine only worked on this way:

success: function (data) {
    response($.map(data.slice (0,10), function(item) {
    return {
    value: item.nome
    };
    }));
},
还给你自由 2024-12-14 06:03:21

就我而言,这工作得很好:

source:function(request, response){
    var numSumResult = 0;
    response(
        $.map(tblData, function(rowData) {
            if (numSumResult < 10) {
                numSumResult ++;
                return {
                    label:          rowData.label,
                    value:          rowData.value,
                }
            }
        })
    );
},

In my case this works fine:

source:function(request, response){
    var numSumResult = 0;
    response(
        $.map(tblData, function(rowData) {
            if (numSumResult < 10) {
                numSumResult ++;
                return {
                    label:          rowData.label,
                    value:          rowData.value,
                }
            }
        })
    );
},
锦上情书 2024-12-14 06:03:21

我将这个库留给使用此库的任何人

JavaScript-autoComplete/1.0.4/auto-complete.js

这可能会有所帮助,因为演示版本不显示如何限制结果。基于安德鲁的回应。

new autoComplete({
    selector: 'input[name="name_of_searchbox"]',
    minChars: 1,
    source: function(term, suggest){
    term = term.toLowerCase();
    var choices = [];
     var matches = []; //For Pushing Selected Terms to be always visible, can be adapter later on based on user searches
      for (i=0; i<choices.length; i++)
          if (~choices[i].toLowerCase().indexOf(term)) matches.push(choices[i]);
      suggest(matches.slice(0,10));
     }
    });

希望这对任何人都有用。
干杯!

I'm leaving this one for anyone who is using this library

JavaScript-autoComplete/1.0.4/auto-complete.js

This might be helpful as the Demo version doesn't show how to limit results. Based on Andrew response.

new autoComplete({
    selector: 'input[name="name_of_searchbox"]',
    minChars: 1,
    source: function(term, suggest){
    term = term.toLowerCase();
    var choices = [];
     var matches = []; //For Pushing Selected Terms to be always visible, can be adapter later on based on user searches
      for (i=0; i<choices.length; i++)
          if (~choices[i].toLowerCase().indexOf(term)) matches.push(choices[i]);
      suggest(matches.slice(0,10));
     }
    });

Hope this is of use to anyone.
Cheers!

比忠 2024-12-14 06:03:21
.ui-menu-item{
    display: none;
}
.ui-menu-item:nth-child(-n+5){
    display: block;
}
.ui-menu-item{
    display: none;
}
.ui-menu-item:nth-child(-n+5){
    display: block;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文