【发布时间】:2018-06-14 16:37:52
【问题描述】:
我有一个包含美国城市数据的 .json 文件。我已成功将数据拉入我的选择框,但它不显示 optgroup 标题。以下是 .json 文件的摘录:
[
[...],
[{
"Index": 16246,
"Display": "Auburn",
"City": "Auburn",
"County": "Lee",
"State": "Alabama",
"StateAbbv": "AL",
"Latitude": 32.5475,
"Longitute": -85.4682,
"Zipcode": 36830
}, {
"Index": 16247,
"Display": "Auburn",
"City": "Auburn",
"County": "Lee",
"State": "Alabama",
"StateAbbv": "AL",
"Latitude": 32.5782,
"Longitute": -85.349,
"Zipcode": 36831
}, {
"Index": 16248,
"Display": "Auburn",
"City": "Auburn",
"County": "Lee",
"State": "Alabama",
"StateAbbv": "AL",
"Latitude": 32.592,
"Longitute": -85.5189,
"Zipcode": 36832
}],
[{
"Index": 16249,
"Display": "Auburn University",
"City": "AuburnUniversity",
"County": "Lee",
"State": "Alabama",
"StateAbbv": "AL",
"Latitude": 32.6024,
"Longitute": -85.4873,
"Zipcode": 36849
}],
[...]
]
如您所见,Auburn 有三个条目,所以我希望“Auburn”成为 optgroup 标题,三个条目(显示邮政编码)位于其下方。我包括了一个单数条目(奥本大学),它也应该以相同的方式显示,但只有一个邮政编码。
这基本上是我在下拉列表中寻找的格式:
AUBURN
36380
36831
36832
AUBURN UNIVERSITY
36849
这是我在页面上设置的插件配置:
$select_city = $('#zipcode-select').selectize({
optgroupField: 'Display',
optgroupLabelField: 'Display',
optgroupValueField: 'Index',
maxItems: zipcodeLimit,
valueField: 'Zipcode',
labelField: 'Zipcode',
searchField: ['Zipcode','Display']
});
目前它只输出邮政编码:
由于我指定在“显示”和“邮政编码”字段中进行搜索,因此我可以搜索邮政编码或城市,它会相应地进行过滤:
根据plugin's usage page,我被引导相信提供optgroup* 选项可以实现我想要做的事情,但显然事实并非如此。
有人知道如何实现我想要做的事情吗?
【问题讨论】:
标签: jquery json selectize.js optgroup