Hunt
Hunt

Reputation: 8435

unable to load dynamic listview in jquery mobile

I have create dynamic listview based on database but the problem is to load the listview i have to refresh the page again.

for example ,

Page A.html has a button and Page B.html has a listview

when i click on button which is inside page A.html page B.html loads but the listview is not visible and when i refresh the B.html again then the listview loads.

Code of page A i.e. index.html when click on findme page B loads

<div data-role="content">   
        <p>
               <a href="findme.html" id="findMeBtn" data-role="button" data-icon="delete">Find Me</a>
               <a href="index.html" data-role="button" data-icon="delete">Fingle Hunts Basic</a>
               <a href="index.html" data-role="button" data-icon="delete">Communicator</a>  

        </p>


        <p>
         <div id="action">
              <div id="loading-icon"></div>
              <div id="msg"></div>
         </div>
        </p>
</div><!-- /content -->

Code of Page B i.e.findme.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Find Me</title>
<link rel="stylesheet" type="text/css" href="style/jquery.mobile.css"/>
<link rel="stylesheet" type="text/css" href="style/main.css"/>

</head>

<body>
    <div data-role="page"  id="findme">
    <div data-role="header"  data-theme="b">
        <a href="index.html" data-icon="back">Back</a>
        <h1>VisitFingal</h1>

    </div><!-- /header -->
    <div data-role="content" >  
    <center>
    <div class="vf-nav-view vf-button-panel" >
        <div data-role="controlgroup" data-type="horizontal" data-fullscreen="true" >
            <a href="index.html" data-role="button" style="width:30%;" data-theme="b">List</a>
            <a href="index.html" data-role="button" style="width:30%;" data-theme="b">Map</a>
        </div>
    </div>
    </center>

    <div id="findMeListView">

    </div>


    </div><!-- End of FindMe Page -->
    <div data-role="page"  id="details">
         <div data-role="header"><h1>Details</h1></div>
        <div data-role="content"></div>
    </div>
    <div data-role="footer"  data-theme="b"  data-id="mainfoot" data-position="fixed">
    <div data-role="navbar">
        <ul>
            <li><a href="index.html" >Home</a></li>
            <li><a href="findme.html"  class="ui-btn-active ui-state-persist">Find Me</a></li>
            <li><a href="#">Communicatior</a></li>
            <li><a href="#">Like</a></li>
            <li><a href="#">Favourates</a></li>
        </ul>
    </div>

    </div><!-- /footer -->

    </div>
</body>
<script src="js/jquery-1.6.3.min.js" type="application/javascript"></script>
<script src="js/jquery.mobile.js"  type="application/javascript"></script>
<script type="application/javascript">

        $(document).bind( "pagebeforechange", function( e, data ) {



          $.getJSON('getData.php', function(data) {
                  var items = [];
                $page  = $("#findme");
                $content = $page.find("#findMeListView" );
                var listViewData='<ul data-role="listview" data-filter="true" data-inset="true" data-theme="c" >';
                for (var i=0; i <data.length;i++)                   
                {
                    listViewData += '<li><a href="details.html?id='+data[i].id+'"><img height="95" width="95" src="'+data[i].url+'" /><h3>'+data[i].name+'</h3></li>';

                }
                     listViewData+='</ul>';

                    $content.html(listViewData);
                    $content.find( ":jqmData(role=listview)" ).listview();



          });


        });

</script>
</html>

Upvotes: 3

Views: 2810

Answers (3)

JaydeepW
JaydeepW

Reputation: 3285

For those of you who want the direct source code to use in your applications, I have created a free and open source repository on Github.

Please note that, it is currently using JQM 1.1.0 Feel free to download, fork, use, copy the code.

https://github.com/jaydeepw/jquerymobile-dynamic-listview

Cheers!

Upvotes: 0

Jasper
Jasper

Reputation: 76003

When you use a multi-page template like this the JS will be ignored when you click from page A to page B. This is because jQuery Mobile AJAX linking pulls just the first data-role="page element from the new page and appends it to the current DOM, as you can imagine this ignores any code outside the data-role="page element.

Two fixes:

  1. Put the JavaScript code for page B directly in the data-role="page element so it will be parsed when jQuery Mobile does an AJAX load of the page.

  2. Put all the code for your site into a single JS include file and include it in the <head> of every page; this way all the code for your site will be available whether the user refreshes or deep-links into the site and when they navigate around using jQuery Mobile's AJAX linking. This is my preferred method.

Upvotes: 5

wwwroth
wwwroth

Reputation: 434

I've had problems with jQuery Mobile before when loading new pages and content not firing correctly. Mostly with form submissions and database calls. I used a cheap fix by putting the attribute data-ajax="false" on the link loading the page. This way it does a full HTTP REQUEST instead of just an ajax call and loads the DOM and everything.

Here's an example..

<a href="findme.html" id="findMeBtn" data-role="button" data-icon="delete" data-ajax="false">Find Me</a>

Upvotes: 0

Related Questions