JavaScript 中的 GroupBy 用于对 JSON 数据进行分组并填充到 optgroup 上
我有点失落。 我得到这个 JSON:
[{
"id": "210",
"name": "Name 1",
"category": "Category 1"
}, {
"id": "187",
"name": "Name 2",
"category": "Category 1"
}, {
"id": "186",
"name": "Name 3",
"category": "Category 1"
}, {
"id": "185",
"name": "Name 4",
"category": "Category 1"
}, {
"id": "184",
"name": "Name 5",
"category": "Category 1"
}, {
"id": "183",
"name": "Name 6",
"category": "Category 1"
}, {
"id": "182",
"name": "Name 7",
"category": "Category 1"
}, {
"id": "181",
"name": "Name 8",
"category": "Category 2"
}, {
"id": "180",
"name": "Name 9",
"category": "Category 3"
}, {
"id": "178",
"name": "Name 10",
"category": "Category 2"
}]
我想将所有这些放入带有选项和 optgroups 的选择中。实际上 optgroup 应该是
我想要这样的类别:
<select name="products" class="product" id="product">
<optgroup label="Category 1">
<option value="210">Name 1</option>
<option value="187">Name 2</option>
<option value="186">Name 3</option>
<option value="185">Name 4</option>
...
</optgroup>
<optgroup label="Category 2">
<option value="181">Name 8</option>
<option value="178">Name 10</option>
</optgroup>
<optgroup label="Category 3">
<option value="180">Name 9</option>
</optgroup>
今天我之所以这么做是因为我太挣扎了:
$(document).ready(function () {
$.getJSON("5.php", {
val: $(this).val()
}, function (data) {
$.each(data, function (i, item) {
$("<option/>").attr("value", item.id).append(item.name).appendTo("optgroup");
});
});
});
正如你所看到的,没有 optgroup :) 有办法做到这一点吗? 如果可以更轻松的话,我还可以修改我的 JSON。
感谢您的任何帮助。
I am a little bit lost.
I am getting this JSON:
[{
"id": "210",
"name": "Name 1",
"category": "Category 1"
}, {
"id": "187",
"name": "Name 2",
"category": "Category 1"
}, {
"id": "186",
"name": "Name 3",
"category": "Category 1"
}, {
"id": "185",
"name": "Name 4",
"category": "Category 1"
}, {
"id": "184",
"name": "Name 5",
"category": "Category 1"
}, {
"id": "183",
"name": "Name 6",
"category": "Category 1"
}, {
"id": "182",
"name": "Name 7",
"category": "Category 1"
}, {
"id": "181",
"name": "Name 8",
"category": "Category 2"
}, {
"id": "180",
"name": "Name 9",
"category": "Category 3"
}, {
"id": "178",
"name": "Name 10",
"category": "Category 2"
}]
And I would like to put all of this in a select with options and optgroups. Actually the optgroup should be category
I would like something like this:
<select name="products" class="product" id="product">
<optgroup label="Category 1">
<option value="210">Name 1</option>
<option value="187">Name 2</option>
<option value="186">Name 3</option>
<option value="185">Name 4</option>
...
</optgroup>
<optgroup label="Category 2">
<option value="181">Name 8</option>
<option value="178">Name 10</option>
</optgroup>
<optgroup label="Category 3">
<option value="180">Name 9</option>
</optgroup>
Today I have only made this because I'm struggling too much:
$(document).ready(function () {
$.getJSON("5.php", {
val: $(this).val()
}, function (data) {
$.each(data, function (i, item) {
$("<option/>").attr("value", item.id).append(item.name).appendTo("optgroup");
});
});
});
As you can see no optgroup :)
Is there a way to do this?
I can also modify my JSON if it can make it easier.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设 optgroup 已经存在,请将其更改
为...
http://jsfiddle.net/FG9Lg/
如果它们不存在,您需要创建它们,但我建议重组您的 JSON 响应,将每个项目嵌套在正确的类别下。
像这样...
所以你可以这样做:
http://jsfiddle.net/FG9Lg/1/
Assuming the optgroups already exist, change this...
to this...
http://jsfiddle.net/FG9Lg/
If they don't exist, you need to create them, though I'd suggest a restructuring of your JSON response to have each item nested under the proper category.
Like this...
So you could then do this:
http://jsfiddle.net/FG9Lg/1/
如果我是你,我会使用一个名为 Underscore 的小型库对返回的数据进行分组以更简单的表示。
请参阅下面的代码,您可能还会看到此现场演示:
如果您犹豫是否使用 Underscore 库,您可以可以考虑这个
groupBy
函数:用法:
If I were you, I would use a small library called Underscore to group the data which is returned in a easier representation.
See this code below and you may also see this live demo:
If you hesitate to use Underscore library, you may consider this
groupBy
function:USAGE:
我知道这个线程很旧,但我需要类似的东西,所以我想出了这个。它会在需要时自动添加选项组并用选项填充它们。另外,当你有 optgroups 和没有 optgroups 时它都可以工作。
http://jsfiddle.net/mzj0nuet/
I know this thread is very old, but I was needing something similar and I came up with this. It automatically adds optgroups when needed and populate them with options. Plus, it works both when you have optgroups and when you don't.
http://jsfiddle.net/mzj0nuet/
如果您想保持 JSON 格式不变,以下内容将回答您的问题:
希望这会有所帮助!
In case you would like to keep you JSON format as is, the following would answer your question:
Hope this helps!