0

I am trying to render a listview on a second page using jquery.mobile.

I am using a single HTML with multiple "pages".

    <div data-role="page" id="foo">
    <a href="#bar" data-role="button" data-inline="true" id="loadList)">List</a>

</div>

<div data-role="page" id="bar">
    <div id="ListDiv">
    <ul id="listview" data-role="listview" data-inset="true">
    </ul>
    </div>

</div>

<!--application UI goes here-->

<script type="text/javascript">
$("#loadList").click(function(){
    $.getJSON(
              "http://localhost/index.php/api/list",{format: "json"},
              function(data){
                  var output = '';
                      $.each(data, function(key, val) {
                          output += '<li><a href="#">' + val +'</a></li>';
                      });
                      $('#listview').append(output).listview('refresh');
                  });
    });

But the problem now is when I click the #loadList button, it does take me to the #bar page, however, it doesn't seem $("#loadList").click() gets executed, because the list doesn't show up on the #bar page. Namely, nothing gets appended to #listView.

Any thoughts?

Maxim Mai
  • 145
  • 5
  • 14

2 Answers2

2

First there's a problem in this code, $.getJSON loading logic is false. Change page can not be used in a same time with $.getJSON.

There are 2 ways you can make this work:

  1. Remove onclick event and load your data during second page initialization:

    $(document).on('pagebeforeshow', '#bar', function(){       
        $.getJSON(
                  "http://localhost/index.php/api/list",{format: "json"},
                  function(data){
                      var output = '';
                          $.each(data, function(key, val) {
                              output += '<li><a href="#">' + val +'</a></li>';
                          });
                          $('#listview').append(output).listview('refresh');
                      });
        });
    });
    

    Unfortunately there's a problem with this solution. Used page event is not going to wait for $.getJSON, so in some cases your listview will be empty when page loads and content will suddenly appear. So this solution is not that great.

  2. Second solution is remove href="#bar" attribute from the button but leave a click event. When $.getJSON action is successful the use changePage function and load next page. In case there's a problem with accessing second page listview store your result (In this ARTICLE you will find how, or HERE) and append it again during a pagebeforeshow event of a #bar page.

I made you a working jsFiddle example: http://jsfiddle.net/Gajotres/GnB3t/

HTML :

<!DOCTYPE html>
<html>
<head>
    <title>jQM Complex Demo</title>
    <meta name="viewport" content="width=device-width"/>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
</head>
<body>
    <div data-role="page" id="index">
        <div data-theme="a" data-role="header">
            <h3>
                First Page
            </h3>
        </div>        
        <div data-role="content">
            <a href="#" data-role="button" id="populate-button">Load JSON data</a>
        </div>
    </div>
    <div data-role="page" id="second">
        <div data-theme="a" data-role="header">
            <h3>
                Second Page
            </h3>
            <a href="#index" class="ui-btn-left">Back</a>
        </div>

        <div data-role="content">
            <h2>Simple list</h2>
            <ul data-role="listview" data-inset="true" id="movie-data" data-theme="a">

            </ul>
        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">

        </div>
    </div>      
</body>
</html>    

JS :

$(document).on('pagebeforeshow', '#index', function(){     
    $('#populate-button').on('click',function(e,data){     
        $.ajax({url: "http://api.themoviedb.org/2.1/Movie.search/en/json/23afca60ebf72f8d88cdcae2c4f31866/The Goonies",
            dataType: "jsonp",
            jsonpCallback: 'successCallback',
            async: true,
            beforeSend: function() {
                $.mobile.showPageLoadingMsg(true);
            },
            complete: function() {
                $.mobile.hidePageLoadingMsg();
            },
            success: function (result) {
                ajax.jsonResult = result;
                $.mobile.changePage("#second");
            },
            error: function (request,error) {
                alert('Network error has occurred please try again!');
            }
        });
    });        
});

$(document).on('pagebeforeshow', '#second', function(){       
    ajax.parseJSONP(ajax.jsonResult);
});


var ajax = {  
    jsonResult : null,
    parseJSONP:function(result){
        $('#movie-data').empty();
        $('#movie-data').append('<li>Movie name:<span> ' + result[0].original_name+ '</span></li>');
        $('#movie-data').append('<li>Popularity:<span> ' + result[0].popularity + '</span></li>');
        $('#movie-data').append('<li>Rating:<span> ' + result[0].rating+ '</span></li>');
        $('#movie-data').append('<li>Overview:<span> ' + result[0].overview+ '</span></li>');
        $('#movie-data').append('<li>Released:<span> ' + result[0].released+ '</span></li>');  
        $('#movie-data').listview('refresh');
    }
}
Community
  • 1
  • 1
Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • Thanks, the second approach works, although it's not the best solution I wanted to have, but it solved my problem. Thanks again. – Maxim Mai Feb 27 '13 at 06:55
0

You have id="loadList)", that is, with a closing parenthesis in

<a href="#bar" data-role="button" data-inline="true" id="loadList)">List</a>

Maybe this is causing your problem?

voyager42
  • 133
  • 8