jquery - JavaScript 中的 GroupBy 用于对 JSON 数据进行分组并填充到 optgroup 上

标签 jquery 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"
}]

我想将所有这些放入带有选项和选项组的选择中。实际上 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。

感谢您的帮助。

最佳答案

假设 optgroup 已经存在,请更改此...

.appendTo("optgroup")

对此...

.appendTo("optgroup[label='" + item.category + "']");

http://jsfiddle.net/FG9Lg/

<小时/>

如果它们不存在,您需要创建它们,但我建议重组您的 JSON 响应,将每个项目嵌套在正确的类别下。

像这样...

{
    "Category 1":[
        {"id": "210","name": "Name 1"},
        {"id": "187","name": "Name 2"},
        {"id": "186","name": "Name 3"},
        {"id": "185","name": "Name 4"},
        {"id": "184","name": "Name 5"},
        {"id": "183","name": "Name 6"},
        {"id": "182","name": "Name 7"}
    ],
    "Category 2":[
        {"id": "181","name": "Name 8"},
        {"id": "178","name": "Name 10"}
    ],
    "Category 3": [
        {"id": "180","name": "Name 9"}
    ]
}

所以你可以这样做:

var product = $('#product');

$.each(data, function (key, cat) {
    var group = $('<optgroup>',{label:key});

    $.each(cat,function(i,item) {
        $("<option/>",{value:item.id,text:item.name})
            .appendTo(group);
    });

    group.appendTo( product );
});

http://jsfiddle.net/FG9Lg/1/

关于jquery - JavaScript 中的 GroupBy 用于对 JSON 数据进行分组并填充到 optgroup 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8578814/

相关文章:

ios - 我的 tableViewCells 没有创建?

python - 类型错误 : can't use a string pattern on a bytes-like object, 接口(interface)

javascript - 在 Internet Explorer 中显示/隐藏选择选项组选项

html - 有没有办法在选择标签中选择 optgroup 的标签?

jQuery 类在输入模糊/事件时淡入淡出

javascript - 如何取消注册选定的 javascript

json - 使用 axios.get 渲染 json 数据

jquery - iOS 错误?使用 jQuery 聚焦时无法更改选择中的文本

javascript - 检查文本字段是否为空或不是jquery

javascript - 检测何时单击 Facebook 点赞按钮