0

I have a listview that's dynamically generated from a JSON file as such:

$.getJSON('test2.json', function(data) {
                var items = [];
                $.each(data, function(key, val) {
                    items.push('<li data-theme="c" data-icon="myapp-arrow" class="test1"><a href="#dataviewer" data-transition="slide"><p>' + key + '</p>' + val + '</a></li>');
                });
                $('<ul/>', {'data-role' : 'listview','id': 'my-new-list', 'data-divider-theme':'a', 'data-inset': 'false',html: items.join('')}).appendTo('.container');
                alert('Here is where the refresh should fire!');
                $("#my-new-list").listview("refresh");
            });

It adds the list items to the page, but it doesn't style them at all. Using Chromes inspector, if I copy and paste the list code into my html document, it styles just fine. What am I doing wrong?

2
  • Alert fires - just noticed the list styles correctly if I navigate to it from another page, but not if the page itself is refreshed. Commented Apr 24, 2012 at 10:49
  • Ok I suspect then it has to do with something else in your code. Can you show us more or create a jsFiddle illustrating the problem. Let me ask you this are you using using dom ready i.e $(function(){ ... }); instead of binding to pageinit? Commented Apr 24, 2012 at 10:57

2 Answers 2

2

Try $('.ui-page').trigger("create");. You can't refresh a listview if it hasn't been created yet.

Sign up to request clarification or add additional context in comments.

Comments

0

Binding the function to a page init and removing the "refresh" from the .listview() method solved my issue. As far as I can tell, any additional list building after this initial function should include ('#page-id').listview(refresh); and it should update just fine.

$( '#searchpage' ).live( 'pageinit',function(){
                $.getJSON('test2.json', function(data) {
                    var items = [];
                    $.each(data, function(key, val) {
                        items.push('<li data-theme="c" data-icon="myapp-arrow" class="test1"><a href="#dataviewer" data-transition="slide"><p>' + key + '</p>' + val + '</a></li>');
                    });
                    $('<ul/>', {'data-role' : 'listview','id': 'my-new-list', 'data-divider-theme':'a', 'data-inset': 'false',html: items.join('')}).appendTo('.container');
                $("#my-new-list").listview();               
                });
            });

Thanks to codaniel for nudging me in the right direction

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.