0

我正在尝试使用真正有帮助的 Mustache.js。

我被困在获得具有特定选项和价值的数组上。

我的 JSON 看起来像这样:

    {
    "departments": [
        {
            "department": [
                {
                    "id": 114,
                    "deptName": "Department 1",
                    "category": [
                        {
                            "id": 127,
                            "catName": "Category Name",
                            "subCategory": []
                        },
                        {
                            "id": 115,
                            "catName": "Category Name",
                            "subCategory": []
                        }
                    ]
                }
            ]
        },
        {
            "department": [
                {
                    "id": 123,
                    "deptName": "Department 2",
                    "category": [
                        {
                            "id": 126,
                            "catName": "Category Name",
                            "subCategory": []
                        },
                        {
                            "id": 124,
                            "catName": "Category Name",
                            "subCategory": []
                        }
                    ]
                }
            ]
        }
    ]
}

要获取主要部门名称,很容易:

JS:

$.ajax({

            url: 'link_to_json',
            dataType: 'json',
            success: function(data) {
                var template = $('#pageHomeTpl').html();
                var html = Mustache.to_html(template, data);
                $('#category-list').html(html);
            }
        });

HTML:

<ul id="category-list">
                <script id="pageHomeTpl" type="text/template">
                    {{#departments}}
                    {{#department}}
                    <li><a href="{{id}}">{{deptName}}</a></li>
                    {{/department}}
                    {{/departments}}
                  </script>
                </ul>

但现在我需要以某种方式从具有特定 ID 的部门获取类别(“类别:”),例如“id”:114。

任何帮助,请。

4

2 回答 2

1

您可以使用 jQuerymap()grep()

    console.log(JSON.stringify(data)); 
var filtereddata = {"departments" : []}; 
var filtereddept= {};
filtereddept.department= $.map(data.departments, function(alldept,indx){
    return $.grep(alldept.department, function(deptobj,indx){
        return deptobj.id==114;
    });
});
filtereddata.departments[0]=filtereddept;
console.log(JSON.stringify(filtereddata));

迭代部门类别的模板:

   <script id="pageHomeTpl" type="text/template">
                    {{#departments}}
                    {{#department}}
                    <li><a href="{{id}}">{{deptName}}</a></li>
                     {{#category}}
                     <ul>
                    <li><a href="{{id}}">{{catName}}</a></li>
                    </ul>
                    {{/category}}
                    {{/department}}
                    {{/departments}}
      </script>
于 2012-06-07T09:39:44.590 回答
0

我没有测试它,但它似乎是工作......

...
success: function(data) {
    var template = $('#pageHomeTpl').html();
    var item = data.departments
    for(key in item){
        if(key == 'id' && item[key] == 114 ){
            var html = Mustache.to_html(template, item);
        }
    }
    $('#category-list').html(html);
}
...

使用 FOR .. IN 语句,您可以在对象或数组中循环查看此链接 js_loop_for_in

于 2012-06-07T08:46:59.527 回答