以列表格式动态显示JSON值

时间:2014-06-05 08:17:07

标签: javascript jquery json

我有一个来自服务器的JSON响应,看起来像(注意:数据可能更多或更少,但结构将是相同的)

{
    "events": [{
        "id": 852157,
        "name": "Adelaide v Brisbane",
        "timezone": "Australia/Darwin",
        "timezoneOffset": 34200,
        "time": 1401987600000,
        "timeUtc": "2014-06-05T20:00:00+09:30",
        "status": "live",
        "wpRef": "852157",
        "parentCategoryId": 7,
        "parentCategoryName": "Australian Rules",
        "categoryId": 9274,
        "categoryName": "AFL R26 Matches",
        "racingEvent": null,
        "raceNumber": null,
        "marketCount": 0,
        "prefix": null,
        "nameSeparator": null,
        "markets": [],
        "result": null
    }, {
        "id": 852217,
        "name": "Geelong v Carlton",
        "timezone": "Australia/Darwin",
        "timezoneOffset": 34200,
        "time": 1401987600000,
        "timeUtc": "2014-06-05T20:00:00+09:30",
        "status": "live",
        "wpRef": "852217",
        "parentCategoryId": 7,
        "parentCategoryName": "Australian Rules",
        "categoryId": 9274,
        "categoryName": "AFL R26 Matches",
        "racingEvent": null,
        "raceNumber": null,
        "marketCount": 0,
        "prefix": null,
        "nameSeparator": null,
        "markets": [],
        "result": null
    }, {
        "id": 852238,
        "name": "Carlton v Hawthorn",
        "timezone": "Australia/Darwin",
        "timezoneOffset": 34200,
        "time": 1401987600000,
        "timeUtc": "2014-06-05T20:00:00+09:30",
        "status": "live",
        "wpRef": "852238",
        "parentCategoryId": 7,
        "parentCategoryName": "Australian Rules",
        "categoryId": 9274,
        "categoryName": "AFL R26 Matches",
        "racingEvent": null,
        "raceNumber": null,
        "marketCount": 0,
        "prefix": null,
        "nameSeparator": null,
        "markets": [],
        "result": null
    }]
}

我正在尝试显示" name"来自JSON的列表格式的属性。通过onClick方法从服务器检索数据。由于返回的JSON数据可能会有所不同(即可能超过3个事件),因此我希望动态显示JSON数据。

列表的HTML视图类似于:

<div id="someID" class="filtering">
    <h2>EVENTS</h2>
    <ul>
        <li><a href="">Name 1</a>
        </li>
        <li><a href="">Name 2</a>
        </li>
        <li><a href="">Name 3</a>
        </li>
    </ul>
    <div class="clear"></div>
</div>

当我在点击事件

上从服务器获取JSON时,这就是我的JS
var navigation = {

    sportSubCategoryBindClick: function (id, parentId) {

        $("#" + id).live('click', function () {

            var thisEl = this,
                categoryId = $(this).attr('id');

            $.when(ajaxCalls.fetchEventsForCategory(id, parentId, days.fromToday)).done(function (eventsMap) {

                // There are no events
                if (eventsMap.events.length == 0) {
                    $('#mainError').show();
                    $('div.main').hide();
                } else {
                    $('#' + categoryId).addClass('active');

                    var events = eventsMap.events;

                    // If there are events
                    if (events.length > 0) {

                        navigation.drawAllEvents(events);

                    }
                }
                progressIndicator(false);
            });
        });
    },

    drawAllEvents: function (events)  {

        console.log(events);
    }
}

我如何动态填充&#34;名称&#34;在HTML标记中提到的列表视图(drawAllEvents函数)中来自JSON的字段。可能我需要使用

$.each(result,function(index, value){
    // code goes over here
});

但我不知道如何在我的案例中使用它。

1 个答案:

答案 0 :(得分:1)

您需要使用从传递的li获取的数据动态创建新的events元素,并将其附加到DOM。像这样:

drawAllEvents: function (events)  {
    var $container = $('#someID ul');
    $.each(events, function(i, event) {
        $('<a />', { text: event.name, href: '#' }).wrap('<li />').parent().appendTo($container);
    });
}

Example fiddle