简体   繁体   English

jQuery Mobile从Listview创建页面

[英]JQuery Mobile Create Page From Listview

Using the code below how would i create a new dynamic page that can be used to show further detailed information about a film? 使用下面的代码,我将如何创建一个新的动态页面,以用于显示有关电影的更多详细信息? i imagine it uses a onclick event handler with a reference to $.mobile.changePage. 我想象它使用一个$ .mobile.changePage引用的onclick事件处理程序。

// JAVASCRIPT

$(document).on('pagebeforeshow', '#moviefilm', function(){
           var apikey = "MYAPIKEY";
           var baseUrl = "http://api.rottentomatoes.com/api/public/v1.0";

           // construct the uri with apikey
           var moviesSearchUrl = baseUrl + '/lists/movies/box_office.json?apikey=' + apikey + '&limit=5' + '&country=us';

           $(document).ready(function() {

                             // send off the query
                             $.ajax({
                                    url: moviesSearchUrl,
                                    dataType: "jsonp",
                                    success: searchCallback
                                    });
                             });

           // callback for when we get back the results
           function searchCallback(data) {
           $(document.body).append('Found ' + data.total + ' results for Top Box Office Earning Movie');
           var movies = data.movies;
           $.each(movies, function (index, movie) {
                  $("#filmlist").append("<li><a><img src='" + movie.posters.thumbnail + "' /><h2>" + movie.title + "</h2>" + "<p>" + "Score: " + movie.ratings.critics_score + "%" + "</p></a></li>").listview().listview("refresh");
                  });
           }
           });


// HTML
         <div data-role="main" class="ui-content">
         <ul data-role="listview" id="filmlist"></ul>
         </div>

Add a delegated click handler for the anchors within the list. 为列表中的锚添加委托的单击处理程序。 On click create the page markup, append it to the body, and call change on the page container widget: 单击时,创建页面标记,将其附加到正文,然后在页面容器小部件上调用change:

var pIdx = 1;
$("#filmlist").on("click", "li a", function(){        
    var newpageid = 'page' + pIdx;
    pIdx++;
    var thetitle = $(this).find("h2").text();
    var phtml = '<div data-role="page" id="' + newpageid + '">';

    phtml += '<div data-role="header"><h1>' + thetitle + '</h1></div> ';
    phtml += '<div data-role="main" class="ui-content">';
    phtml += '</div></div>';
    $("body").append(phtml);

    $(":mobile-pagecontainer").pagecontainer( "change", "#" + newpageid, { transition: "slide" } );
 });

DEMO 演示

UPDATE: 更新:

Instead of creating a new page for each movie, it is better to have one detail page and just update its contents for each movie that is clicked. 与其为每个电影创建一个新页面,不如拥有一个详细信息页面并为每个被单击的电影更新其内容,这是更好的选择。 Add a second page in your markup and change the click handler: 在您的标记中添加第二页并更改点击处理程序:

<div data-role="page" id="headline">
    <div data-theme="a" data-role="header">
        <a href="#moviefilm" class="ui-btn-left" data-transition="slide" data-direction="reverse">Back</a>                        
        <h3>
            Movie Details
        </h3>
    </div>        
    <div data-role="main" class="ui-content">
        <h3 id="theMovieTitle"></h3>
        <p>movie details go here</p>
    </div>
</div>   

$("#filmlist").on("click", "li a", function(){          
    var thetitle = $(this).find("h2").text();
    $("#theMovieTitle").text(thetitle);        
    $(":mobile-pagecontainer").pagecontainer( "change", "#headline", { transition: "slide" } );
 }); 

Updated DEMO 更新了演示

As for getting the movie details content, you will need something in the listview that uniquely identifies the movie (ID, title, etc.). 至于获取电影详细信息的内容,您将需要在列表视图中唯一标识电影的内容(ID,标题等)。 Then on click, use this unique identifier to get the details and put them into the second page contents. 然后单击,使用此唯一标识符获取详细信息,并将其放入第二页内容中。

Is there a simpler way? 有没有更简单的方法? That looks a little complex to me. 在我看来,这有点复杂。 I was looking for something like this: http://jsfiddle.net/Gajotres/8uac7/ but using my code above. 我一直在寻找这样的东西: http : //jsfiddle.net/Gajotres/8uac7/,但是使用上面的代码。 Thanks 谢谢

[1]: http://jsfiddle.net/Gajotres/8uac7/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM