使用本地 JSON 数据填充 jQuery Mobile ListView [英] Populating jQuery Mobile ListView with local JSON data

查看:30
本文介绍了使用本地 JSON 数据填充 jQuery Mobile ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用本地 JSON 信息填充 JQM ListView.但是,不会创建任何列表项.任何帮助,将不胜感激.这是我的代码:

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:

JSON 文件结构:

HTML:

HTML:
<ul data-role="listview" data-inset="true" id="searchFood">

<div data-role="page" data-title="Search" id="searchPage"> <ul data-role="listview" data-inset="true" id="searchFood"> </ul> </div>

JS:

(更新)


推荐答案

首先,返回的JSON数组是错误的,值(属性)应该用逗号隔开.

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()函数返回的value对象,而不是data数组.

$.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. 

jQueryMobile 仅在页面加载时增强一次页面.当新数据动态添加到页面时,jQueryMobile 必须知道要增强的数据的数据.

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

JSON 数组中提取数据后,附加它们然后 refresh listview 以重新设置新添加的元素的样式.

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



<块引用>

演示

这篇关于使用本地 JSON 数据填充 jQuery Mobile ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆