jQuery自动完成组结果具有相同的名称

时间:2012-10-15 07:41:30

标签: jquery autocomplete

我将帖子绑定到某个位置,因此当我在该位置上生成jQuery自动完成时,下拉列表中会显示具有相同名称的结果。

例如:

如果有10个位置柏林的帖子,我开始输入城市名称,自动填充将给我的位置名称乘以帖子数量。我想知道是否有办法使用jQuery对位置名称进行分组。

由于

1 个答案:

答案 0 :(得分:2)

This jquery ui demo 与您的要求类似。

原始数据:

var data = [
                { label: "anders", category: "" },
                { label: "andreas", category: "" },
                { label: "antal", category: "" },
                { label: "annhhx10", category: "Products" },
                { label: "annk K12", category: "Products" },
                { label: "annttop C13", category: "Products" },
                { label: "anders andersson", category: "People" },
                { label: "andreas andersson", category: "People" },
                { label: "andreas johnson", category: "People" }
            ];

您可以将其更改为

 var data = [
                { label: "Berlin", category: [post1, post2] },
                { label: "Paris", category: [post3, post4, post5] },
                { label: "Tokyo", category: [post6, post7, post8] },
                { label: "Taipei", category: [] }
                ...
      ];

检索值

select: function( event, ui ) {
                console.log( ui.item.label );
                console.log( ui.item.category ); //this is an array
                return false;
}

http://jqueryui.com/autocomplete/#custom-data 是关于dropmenu的自定义数据格式的演示

我希望这个示例有用。