Populating jQuery Mobile ListView with local JSON data

后端 未结 1 2074
长发绾君心
长发绾君心 2020-11-29 13:46

I am trying to populate a JQM ListView with a local JSON information. However, no list items are created. Any help would be appreciated. Here is my code:

JS

相关标签:
1条回答
  • 2020-11-29 14:42

    First of all, the return JSON array is wrong, values (properties) should be separated by commas.

    var data = [{
        "name": "test",
            "calories": "1000",
            "fat": "100",
            "protein": "100",
            "carbohydrates": "800",
    }, {
        "name": "test2",
            "calories": "10000",
            "fat": "343",
            "protein": "3434",
            "carbohydrates": "4343",
    }];
    

    Second mistake, you should read value object returned by $.each() function not data array.

    $.each(data, function (index, value) {
      output += '<li><a href="#">' + value.name + '</a></li>';
    });
    

    jQueryMobile only enhances the page once when it is loaded. When new data is added dynamically to the page, jQueryMobile must be made aware of the data for the data to be enhanced.

    After extracting data from JSON array, append them then refresh listview to restyle newly added elements.

    $('#searchFood').html(output).listview("refresh");
    

    Demo

    0 讨论(0)
提交回复
热议问题