Search code examples
jquerysqlcordovajquery-mobilejquery-mobile-listview

Binding data to collapsible set in phonegap


i have a collapsible set to which i am trying to the append query result as the listview. but i am unable to bind the result as listview.

This is what i am trying to do

function queryDB(tx) {
    tx.executeSql('SELECT * FROM FOLDER', [], querySuccess, errorCB);
}

function listdata(tx,results){
    list= ("<ul data-role='listview' data-inset='false' id='mylist' />");
    count = results.rows.length;
    $.each(results.rows,function(index){ 
        var row = results.rows.item(index);
        var li = '<li><a href="#">'+row['Date']+'</a></li>';
        list = $(list).append(li);
    });   
}

function querySuccess(tx, results) {    
    $.each(results.rows,function(index){ 
        var row = results.rows.item(index);

        tx.executeSql('SELECT Date FROM ALLIGNMENT WHERE name="'+row[name]+'"', [], listdata, errorCB);

        var div = '<div data-role="collapsible" data-inset="false" data-iconpos="right" data-collapsible="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d"><h3>'+
            row["name"]+'<span class="ui-li-count ui-btn-up-c ui-btn-corner-all" data-iconpos="right">10</span></h3></div>';

        $(list).appendTo(div).parent().appendTo('[data-role="content"]').end().trigger("create");
        $('div[data-role="collapsible"]').collapsible({theme:'c',refresh:true});
        $('[data-role="listview"]').listview().listview('refresh'); 
    });
} 

If i try to display just a collapsible set, i am able to display. but when i try to display the listview nothing is getting displayed. what is the mistake i am doing?

Thanks:)

New function structure

function queryDB(tx) {
tx.executeSql('SELECT * FROM FOLDER', [], querySuccess, errorCB);
}




 function listdata(tx,resultset){

   list = $("<ul>").attr({'data-role':'listview','data-inset':'false','id':'mylist'});
   count = resultset.rows.length;

    $(list).remove();

   $.each(resultset.rows,function(index){
    var row = resultset.rows.item(index);
   // alert(row['CreatedDate']);
     var li = '<li><a href="#">'+row['Date']+'</a></li>';
    list.append(li); 

    });



div = '<div data-role="collapsible" data-inset="false" data-iconpos="right" data-collapsible="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d"><h3>'+
 row1["name"]+'<span class="ui-li-count ui-btn-up-c ui-btn-corner-all" data-iconpos="right">'+count+'</span></h3></div>';
 }
list.appendTo(div).parent().appendTo('[data-role="content"]').end().trigger("create");
$('div[data-role="collapsible"]').collapsible({theme:'b',refresh:true});
$('[data-role="listview"]').listview().listview('refresh'); 

 }


function querySuccess(tx, results) {

$.each(results.rows,function(index){ 

   row1 = results.rows.item(index);

tx.executeSql('SELECT CreatedDate FROM ALLIGNMENT WHERE name="'+row1["name"]+'" ', [], listdata, errorCB);

});

} 

Solution

  • You have an error in your function listdata.

    jQuery object can't be created like this:

    list= ("<ul data-role='listview' data-inset='false' id='mylist' />");
    

    For start you are missing $ sign before ( but even with that it will not work.

    Do it like this:

    var list = $("<ul>").attr({'data-role':'listview','data-inset':'false','id':'mylist'});
    

    Also in this case you don't need to use this line like this:

    list = $(list).append(li);
    

    it will not work, it should look like this:

    list.append(li);
    

    EDIT :

    You will need to make addition changes. When you execute this line:

    tx.executeSql('SELECT Date FROM ALLIGNMENT WHERE name="'+row[name]+'"', [], listdata, errorCB);
    

    call to function listdata is asynchronous. Which means that rest of the code will not wait for this function to run. Basically you will need to move some of this lines:

        var div = '<div data-role="collapsible" data-inset="false" data-iconpos="right" data-collapsible="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d"><h3>'+
        row["name"]+'<span class="ui-li-count ui-btn-up-c ui-btn-corner-all" data-iconpos="right">10</span></h3></div>';
    
        $(list).appendTo(div).parent().appendTo('[data-role="content"]').end().trigger("create");
        $('div[data-role="collapsible"]').collapsible({theme:'c',refresh:true});
        $('[data-role="listview"]').listview().listview('refresh'); 
    

    into the function listdata. Currently list is empty because function is still running when you try to append list object.