开发者

Can't append list item to list view in jQuery mobile

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-029baa64356c5187开发者_Go百科610d9867c590921b","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}}
]}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜