Wednesday, April 11, 2012

jQuery Mobile - Create List items dynamically

Refer to the another post of Create List items dynamically for normal Javascript version. We can create List items similarly. But after list items updated, have to call $("#unorderedlist").listview("refresh") to refresh in mobile style.

example:



<!doctype html>
<html>
  <head>
   <meta charset="utf-8"></meta>
   <meta name = "viewport" content = "width = device-width, height = device-height" />
   <title>Android-er</title>
   
 <link rel="stylesheet"  href="http://code.jquery.com/mobile/1.1.0-rc.2/jquery.mobile-1.1.0-rc.2.min.css" />  
 <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
 <script src="http://code.jquery.com/mobile/1.1.0-rc.2/jquery.mobile-1.1.0-rc.2.min.js"></script>

    <script >
    
    var weekdayarray = new Array ("Sunday",
          "Monday",
          "Tuesday",
          "Wednesday",
          "Thursday",
          "Friday",
          "Saturday");
          
    function onloaded(){
     var ulist = document.getElementById("unorderedlist");
     
     //Generate list items for <ul>
     for (var i = 0; i < weekdayarray.length; i++) {
      var li = document.createElement("li");
      li.innerHTML = weekdayarray[i];
      ulist.appendChild(li);
     }
     $("#unorderedlist").listview("refresh");
    }
    </script>
  </head>
  <body onload="onloaded()">
  
   <div data-role="page" id="home">
    <div data-role="header">
     <h1>Mobile-Web-App</h1>
    </div>
    
    <div data-role="content">
     <ul data-role="listview" id="unorderedlist" />
    </div>
   
    <div data-role="footer">
     <h4><a href="http://mobile-web-app.blogspot.com/">http://mobile-web-app.blogspot.com/</a></h4>
    </div>
   </div>
   
  </body>
</html>

Without $("#unorderedlist").listview("refresh"), it will look like this:



No comments:

Post a Comment