无法将列表项附加到 jQuery Mobile 中的列表视图

发布于 2024-12-05 02:53:00 字数 2160 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾城如夢未必闌珊 2024-12-12 02:53:01

请使用 jQM Beta 3 进行测试

而不是这个

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');
}

尝试这个

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));
}

// Make the refresh after the for loop
$('#sku_list').listview('refresh');

// This might be another way but refreshes the whole page
// $('#view_skus').trigger('create');

Please test with jQM Beta 3

Instead of this

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');
}

Try this

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));
}

// Make the refresh after the for loop
$('#sku_list').listview('refresh');

// This might be another way but refreshes the whole page
// $('#view_skus').trigger('create');
原来是傀儡 2024-12-12 02:53:01

我意识到上面的代码是正确的,只是它不应该是 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 been response.rows[i].value.some_property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文