1

在 CodeIgniter 中,在 PHP 中为下拉菜单创建一个类似嵌套的元素,我正在<select>使用这个函数:

function list_categories(&$result, $cats, $sub = ''){    // <- THIS
    foreach($cats as $cat){

        //$cat['category']->id = $sub.$cat['category']->title;
        $result[$cat['category']->id] = $sub.$cat['category']->title; // <- THIS

        if( sizeof($cat['children']) > 0 ){
            $sub2 = str_replace('&mdash;&rarr;&nbsp;', '&ndash;', $sub);
            $sub2.= '&ndash;&rarr;&nbsp;';

            list_categories($result, $cat['children'], $sub2); // <- THIS
        }
    }
}
// Make an array available outside the function
$categoryData = array(0 => '-- '.lang('FORM_SELECT_CATTOP').' --');
// Launch the function to fill out the array with nested sets
list_categories($categoryData, $categories);

生成 HTML<select>列表:

类别

我想从 jQuery 中检索到的 JSON 做同样的事情,但找不到比使用这个 jQuery 函数的 JSON 对象的第一级更深入的答案:

$.getJSON('../raw_get_categories/'+com_id_selected+'/0', function(data){
    var items = [];

    $.each(data, function(key, value, sub){
        items.push('<option id="'+ key[0] + '">' + value[0] + '</option>');
    });

    console.log(items);
});

这是我想要管理并从中检索相同值的 JSON,就像在 PHP 代码中一样:

{
    "28": {
        "category": {
            "id": "28", // <<< I need to retrieve THIS
            "pid": "0",
            "com_id": "2",
            "route_id": "59",
            "excerpt": "",
            "ordering": "1",
            "title": "Oblecenie a vybava",
            "slug": "oblecenie-a-vybava1",
            "description": "",
            "image": "clothes.png",
            "seo_title": "",
            "meta": ""
        },
        "compname": {
            "id": "2",
            "name": "E-commerce"
        },
        "children": {
            "61": {
                "category": {
                    "id": "61", // <<< I need to retrieve THIS
                    "pid": "28",
                    "com_id": "2",
                    "route_id": "52",
                    "excerpt": "",
                    "ordering": "1",
                    "title": "Ubor na hlavu",
                    "slug": "ubor-na-hlavu1",
                    "description": "<p>Capice, Klobuky, Panamy, Baretky, Prilby, Kukly<br></p>",
                    "image": "clothes-head.png",
                    "seo_title": "",
                    "meta": ""
                },
                "compname": {
                    "id": "2",
                    "name": "E-commerce"
                },
                "children": [ ]
            },
            "30": {
                "category": {
                    "id": "30",
                    "pid": "28",
                    "com_id": "2",
                    "route_id": "53",
                    "excerpt": "",
                    "ordering": "2",
                    "title": "Bundy a vetrovky",
                    "slug": "bundy-a-vetrovky",
                    "description": "",
                    "image": "",
                    "seo_title": "",
                    "meta": ""
                },
                "compname": {
                    "id": "2",
                    "name": "E-commerce"
                },
                "children": {
                    "34": {
                        "category": {
                            "id": "34",
                            "pid": "30",
                            "com_id": "2",
                            "route_id": "0",
                            "excerpt": "",
                            "ordering": "0",
                            "title": "Letne",
                            "slug": "letne",
                            "description": "",
                            "image": "",
                            "seo_title": "",
                            "meta": ""
                        },
                        "compname": {
                            "id": "2",
                            "name": "E-commerce"
                        },
                        "children": [ ]
                    },
                    "35": {
                        "category": {
                            "id": "35",
                            "pid": "30",
                            "com_id": "2",
                            "route_id": "0",
                            "excerpt": "",
                            "ordering": "0",
                            "title": "Vsesezonne",
                            "slug": "vsesezonne",
                            "description": "",
                            "image": "",
                            "seo_title": "",
                            "meta": ""
                        },
                        "compname": {
                            "id": "2",
                            "name": "E-commerce"
                        },
                        "children": [ ]
                    },
                    "33": {
                        "category": {
                            "id": "33",
                            "pid": "30",
                            "com_id": "2",
                            "route_id": "0",
                            "excerpt": "",
                            "ordering": "0",
                            "title": "Zimne",
                            "slug": "zimne",
                            "description": "",
                            "image": "",
                            "seo_title": "",
                            "meta": ""
                        },
                        "compname": {
                            "id": "2",
                            "name": "E-commerce"
                        },
                        "children": [ ]
                    }
                }
            },
            "31": {
                "category": {
                    "id": "31",
                    "pid": "28",
                    "com_id": "2",
                    "route_id": "54",
                    "excerpt": "",
                    "ordering": "3",
                    "title": "Nohavice a teplaky",
                    "slug": "nohavice-a-teplaky",
                    "description": "",
                    "image": "",
                    "seo_title": "",
                    "meta": ""
                },
                "compname": {
                    "id": "2",
                    "name": "E-commerce"
                },
                "children": [ ]
            },
            "32": {
                "category": {
                    "id": "32",
                    "pid": "28",
                    "com_id": "2",
                    "route_id": "55",
                    "excerpt": "",
                    "ordering": "4",
                    "title": "Obuv",
                    "slug": "obuv",
                    "description": "",
                    "image": "",
                    "seo_title": "",
                    "meta": ""
                },
                "compname": {
                    "id": "2",
                    "name": "E-commerce"
                },
                "children": [ ]
            }
        }
    },
    "38": {
    ...
}

有人可以建议我foreach在 jQuery pls 中实现相同语句的适当方法吗?

4

2 回答 2

3

Juste使用积分来获得更多孩子

$.getJSON('filename.php', function(data) {
    $('#List li').remove();
    $.each(data, function(index, item) {
        $('#list').append('<li>'+item.category.id+'</li>');
    });
});
于 2013-05-28T10:58:17.693 回答
0

你可以这样做:

$.each(data.actions, function(entryIndex, entry) {
  var html = '<li class="top-level">' + this.action + '</li>';
});

资源

于 2013-05-28T09:19:27.193 回答