Jquery UI 自动完成 - 显示首选的方式

发布于 2024-09-28 07:54:38 字数 1425 浏览 1 评论 0原文

我是一名新手程序员/设计师,试图配置 jquery ui 自动完成。我使用 javascript 对象(数组)让它工作。该数组包含我们引导客户前往的零售商店。我们有某些首选商店,因此我们希望它们比其他商店更早出现,但我真的不知道如何将它们过滤到结果中,或者是否可能。

如果有人输入“Widget”,我希望“Widget Store 4”首先出现。这是 jquery 代码:

var widgetstores = [ 
{label: "Widget Store 1", value: "1001" }, {label: "Widget Store 2", value: "1002" }, {label: "Widget Store 3", value: "1003" }, {label: "Widget Store 4", value: "1004" }, {label: "Widget Store 5", value: "1005" }, {label: "Widget Store 6", value: "1006" }
]

 $(function() {     
                $('#tags').autocomplete({
                    minLength: 3,
                    source: widgetstores,
                    focus: function(event, ui) {
                        $('#tags').val(ui.item.label);
                        return false;
                    },
                    select: function(event, ui) {
                        $('#tags').val(ui.item.label);
                        $('#customer_num').val(ui.item.value);                      
                        return false;
                    }
                })
                .data( "autocomplete" )._renderItem = function( ul, item ) {
                    return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( "<a>" + item.label + "</a>" )
                        .appendTo( ul );
                };
            });

I'm a newbie programmer / designer trying to configure jquery ui autocomplete. I have it working, using a javascript object (array). The array contains retailer stores that we are directing our customers to. We have certain stores that are preferred and so we would like for them to come up earlier than other stores but I don't really know how to filter them into the result or if it is even possible.

I want "Widget Store 4" to appear first if anyone types in "Widget". Here's the jquery code:

var widgetstores = [ 
{label: "Widget Store 1", value: "1001" }, {label: "Widget Store 2", value: "1002" }, {label: "Widget Store 3", value: "1003" }, {label: "Widget Store 4", value: "1004" }, {label: "Widget Store 5", value: "1005" }, {label: "Widget Store 6", value: "1006" }
]

 $(function() {     
                $('#tags').autocomplete({
                    minLength: 3,
                    source: widgetstores,
                    focus: function(event, ui) {
                        $('#tags').val(ui.item.label);
                        return false;
                    },
                    select: function(event, ui) {
                        $('#tags').val(ui.item.label);
                        $('#customer_num').val(ui.item.value);                      
                        return false;
                    }
                })
                .data( "autocomplete" )._renderItem = function( ul, item ) {
                    return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( "<a>" + item.label + "</a>" )
                        .appendTo( ul );
                };
            });

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

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

发布评论

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

评论(1

一场春暖 2024-10-05 07:54:38

我不确定您使用的是什么 AutoComplete 库,但它看起来像:

http://docs.jquery .com/UI/Autocomplete

该库不会自动为您对项目进行排序。它使用传递给脚本的项目的顺序,并从中进行过滤。因此,如果您的项目在传递到自动完成之前按照您想要的顺序排列,那么它应该使用该顺序。

更新:

var widgetstores = [ 
{label: "Widget Store 4", value: "1004" }, {label: "Widget Store 1", value: "1001" }, {label: "Widget Store 2", value: "1002" }, {label: "Widget Store 3", value: "1003" }, {label: "Widget Store 5", value: "1005" }, {label: "Widget Store 6", value: "1006" }
]

I'm not sure what AutoComplete library you're using, but it looks like:

http://docs.jquery.com/UI/Autocomplete

That library does not sort the items for you automatically. It uses the order of the items passed to the script, and filters from that. So if your items are in the order you want them before you pass to the autocomplete it should use that order.

Update:

var widgetstores = [ 
{label: "Widget Store 4", value: "1004" }, {label: "Widget Store 1", value: "1001" }, {label: "Widget Store 2", value: "1002" }, {label: "Widget Store 3", value: "1003" }, {label: "Widget Store 5", value: "1005" }, {label: "Widget Store 6", value: "1006" }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文