nathi
nathi

Reputation: 33

how to load iscroll for dynamic div in android phonegap

I have created application in android phonegap.In my application I have appended list in div dynamicallyloaded . how to add scrollbar for dynamic div using iscroll. I want to add scrollbar for dynamic div using iscroll.how to do this My code is

Html:

         <div class="sidemenu" ></div>

       </div>

Style:

#scroller 
{
    top:90px;
    position:absolute; z-index:1;
    width:90%;
    height:40px;
    background-color:#FFFFFF;
}
li
{
list-style:none;
}

Script:

iscroll:

  var myScroll;
        function loaded() {
            myScroll = new iScroll('scroller', {
                useTransform: false,
                vScroll: true,
                onBeforeScrollStart: function (e) {
                    var target = e.target;
                    while (target.nodeType != 1) target = target.parentNode;

                    if (target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA')
                        e.preventDefault();
                }
            });
        }
        document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
        document.addEventListener('DOMContentLoaded', setTimeout(function () { loaded(); }, 0), false);


       /** scrollbar for dynamic div*/ 
    function doIscrollRefresh () {  
        setTimeout(function () {
            myScroll.refresh();
        }, 0);

    } 

dynamic div: I have retrived value from localdatabase and appended into div dynamically

function list1(alphabet)
{
              doIscrollRefresh ();
             document.addEventListener("deviceready", onDeviceReady, false);
             function onDeviceReady() 
             {

                    var db = window.openDatabase("createdb", "1.0", "PhoneGap Demo", 200000);

                    db.transaction(queryDB, errorCB,successCB);

              } 
              function queryDB(tx) 
             {
                     var a=alphabet;
                     tx.executeSql("SELECT Textvalue FROM TextValue WHERE Textvalue LIKE '" + a + "%'",[], querySuccess, errorCB);
             }

             function querySuccess(tx,results)
              {
                   $(".sidemenu").append("<li>"+"<label style='background-color:#C1CDD9' >"+alphabet+ "</label>"+"</li>");
                   for (i = 0; i < results.rows.length; i++) 
                    {
                         $(".sidemenu").append("<li>"+"<a id='"+i+"'  href='#'>" +results.rows.item(i).Textvalue +  "</a>"+ "<hr/>"+ "</li>");
                    }
                    $(".sidemenu").append("<hr/>");
               }
          function errorCB(err) 
          {
                      alert("Error processing SQL: "+err.code);
          }
          function successCB() 
          {
                 //alert("appended");   
          }
}

It show the error in logcat.the error is myScroll is undefined is not an object. Please tell me the solution.how to addthe scrollbar for dynamic div.please guide me.thanks in advance

Upvotes: 0

Views: 2155

Answers (2)

user3829430
user3829430

Reputation: 11

hi im not shure that will be the thing but if iscroll say that your iscroll is not define its because you call the loaded(); function at start and instance your dynamique code after that, so iscroll cant find your ellement !

i suggest you to callback the loaded function after your dynamique div is display and wrap

just recall the loaded(); function or use iScroll.refresh(); function when your ellement is already display .

  function querySuccess(tx,results)
          {
               $(".sidemenu").append("<li>"+"<label style='background-color:#C1CDD9' >"+alphabet+ "</label>"+"</li>");
               for (i = 0; i < results.rows.length; i++) 
                {
                     $(".sidemenu").append("<li>"+"<a id='"+i+"'  href='#'>" +results.rows.item(i).Textvalue +  "</a>"+ "<hr/>"+ "</li>");
                }
                $(".sidemenu").append("<hr/>");  
   ********************************************************************
      loaded();

/* or you can use*/

     myScroll.refresh();

 *********************************************************************
           }

the important thing to remind is to call or refresh the iscroll after your content is display and have style

Good luck !

Upvotes: 0

Swapnil Godambe
Swapnil Godambe

Reputation: 2044

.page {
    top: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.header {
    position: absolute;
    top: 0;
    height: 42px;
    width: 100%;
    color: #333;
    text-shadow: 0 1px 0 white;
    border: 1px solid #F7C942;
    border-left-width: 0;
    border-right-width: 0;
    background: #FADB4E;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#FCEDA7), to(#FBEF7E));
    background-image: -webkit-linear-gradient(#FCEDA7, #FBEF7E);
    background-image: -moz-linear-gradient(#FCEDA7, #FBEF7E);
    background-image: -ms-linear-gradient(#FCEDA7, #FBEF7E);
    background-image: -o-linear-gradient(#FCEDA7, #FBEF7E);
    background-image: linear-gradient(#FCEDA7, #FBEF7E);
    z-index: 2;
}

.content {
    position: absolute;
    top: 42px;
    bottom: 66px;
    padding-left: 4%;
    padding-right: 4%;
    width: 92%;
    border-width: 0;
    overflow: hidden;
    color: #333;
    text-shadow: 0 1px 0 white;
    z-index: 1;
}

.scroller {
    position: relative;
    overflow: hidden;
    z-index: 1;
}

.footer {
    position: absolute;
    bottom: 0;
    height: 64px;
    width: 100%;
    z-index: 2;
    border: 1px solid #B3B3B3;
    border-left-width: 0;
    border-right-width: 0;
    background: #EEE;
    color: #3E3E3E;
    font-weight: bold;
    text-shadow: 0 1px 1px white;
    background-image: -webkit-linear-gradient(#F0F0F0, #DDD);
    background-image: -moz-linear-gradient(#F0F0F0, #DDD);
    background-image: -ms-linear-gradient(#F0F0F0, #DDD);
    background-image: -o-linear-gradient(#F0F0F0, #DDD);
    background-image: linear-gradient(#F0F0F0, #DDD);
}

var Page = document.createElement('div');
    $(Page).attr('id', 'City');
    $(Page).addClass('page');

var Page = document.createElement('div');
    $(Page).attr('id', 'City');
    $(Page).addClass('page');

    //Header
    var Header = document.createElement('div');
    $(Header).addClass('header');
    $(Header).attr('align', 'center');

//Content
    var Content = document.createElement('div');
    $(Content).addClass('content');
    $(Content).attr('id', 'Wrapper');

    //Scroller
    var Scroller = document.createElement('div');
    $(Scroller).addClass('scroller');

Content.appendChild(Scroller);

    //Footer
    var Footer = document.createElement('div');
    $(Footer).addClass('footer');

    Page.appendChild(Header);
    Page.appendChild(Content);
    Page.appendChild(Footer);

    document.body.appendChild(Page);

    myScroll = new iScroll("Wrapper");

    setTimeout(function() {
        myScroll.refresh();
    }, 500);

Upvotes: 2

Related Questions