无法将列表项附加到 jQuery Mobile 中的列表视图
我的 JavaScript 有什么问题吗?我已经遵循此处有关将数据附加到 jQuery 移动列表视图的问答,但这似乎不起作用。
<!-- start view_skus -->
<div data-role="page" id="view_skus">
<div data-role="header" data-position="fixed">
<a data-role="button" data-rel="back" data-icon="arrow-l">Back</a>
<h1>View SKUs</h1>
<ul data-role="listview" id="sku_list" data-inset="true"></ul>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://matkiros.cloudant.com/sm/_design/view_skus/_view/view_skus");
xhr.onload = function() {
var response = jQuery.parseJSON(xhr.responseText);
for (var i = 0; i < response.total_rows; i++) {
$('#sku_list').append($("<li></li>").html('<h3>' +
rows[i].value.description + '</h3><br/>' +
'ID: ' + rows[i].value.sku_id + '<br/>' +
'Quantity: ' + rows[i].value.quantity))
.listview('refresh');
}
};
xhr.send();
</script>
</div>
</div> <!-- end view_skus -->
基本上,SKU 只是杂货店中的一种产品,我想在列表视图中显示其详细信息。我正在从 URL http://matkiros.cloudant.com/sm/_design/view_skus/_view/view_skus
从 CouchDB 数据库检索 SKU 列表,该 URL 返回以下 JSON 字符串:
{"total_rows":3,"offset":0,"rows":[
{"id":"96ba7296fb95fb14e54a2ae9777dee06","key":"00001","value":{"_id":"96ba7296fb95fb14e54a2ae9777dee06","_rev":"1-029baa64356c5187610d9867c590921b","type":"sku","sku_id":"00001","description":"Dried mangoes","quantity":100}},
{"id":"7e1ec756e85f0e073ed98f3e8a59ebb8","key":"53911","value":{"_id":"7e1ec756e85f0e073ed98f3e8a59ebb8","_rev":"1-5e02bd021232b3bc3bd56dd00d50b64d","type":"sku","sku_id":"53911","description":"Samsung Galaxy S II","quantity":35}},
{"id":"e5b24f49ac538e5187e382a341ac043c","key":"A91J7","value":{"_id":"e5b24f49ac538e5187e382a341ac043c","_rev":"3-f3bab0f8936023c4133f47b29e78c575","type":"sku","sku_id":"A91J7","description":"Jansport bag J2203-1","quantity":50}}
]}
What's wrong with my JavaScript? I've followed the Q&A's here on appending data to jQuery mobile listviews but this doesn't seem to work.
<!-- start view_skus -->
<div data-role="page" id="view_skus">
<div data-role="header" data-position="fixed">
<a data-role="button" data-rel="back" data-icon="arrow-l">Back</a>
<h1>View SKUs</h1>
<ul data-role="listview" id="sku_list" data-inset="true"></ul>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://matkiros.cloudant.com/sm/_design/view_skus/_view/view_skus");
xhr.onload = function() {
var response = jQuery.parseJSON(xhr.responseText);
for (var i = 0; i < response.total_rows; i++) {
$('#sku_list').append($("<li></li>").html('<h3>' +
rows[i].value.description + '</h3><br/>' +
'ID: ' + rows[i].value.sku_id + '<br/>' +
'Quantity: ' + rows[i].value.quantity))
.listview('refresh');
}
};
xhr.send();
</script>
</div>
</div> <!-- end view_skus -->
Basically, an SKU is just a product in a grocery, and I want to display its details in a listview. I'm retrieving the list of SKUs from a CouchDB database from the URL http://matkiros.cloudant.com/sm/_design/view_skus/_view/view_skus
which returns the following JSON string:
{"total_rows":3,"offset":0,"rows":[
{"id":"96ba7296fb95fb14e54a2ae9777dee06","key":"00001","value":{"_id":"96ba7296fb95fb14e54a2ae9777dee06","_rev":"1-029baa64356c5187610d9867c590921b","type":"sku","sku_id":"00001","description":"Dried mangoes","quantity":100}},
{"id":"7e1ec756e85f0e073ed98f3e8a59ebb8","key":"53911","value":{"_id":"7e1ec756e85f0e073ed98f3e8a59ebb8","_rev":"1-5e02bd021232b3bc3bd56dd00d50b64d","type":"sku","sku_id":"53911","description":"Samsung Galaxy S II","quantity":35}},
{"id":"e5b24f49ac538e5187e382a341ac043c","key":"A91J7","value":{"_id":"e5b24f49ac538e5187e382a341ac043c","_rev":"3-f3bab0f8936023c4133f47b29e78c575","type":"sku","sku_id":"A91J7","description":"Jansport bag J2203-1","quantity":50}}
]}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请使用 jQM Beta 3 进行测试
而不是这个
尝试这个
Please test with jQM Beta 3
Instead of this
Try this
我意识到上面的代码是正确的,只是它不应该是
rows[i].value.some_property
,而应该是response.rows[i].value.some_property
。I realized my code above was right, except that instead of saying
rows[i].value.some_property
, it should have beenresponse.rows[i].value.some_property
.