I am using Phonegap and jquery-Mobile-1.4.5 to create a cross platform App. Since I am a Java developer , Java script is a very new concept for mine. I am creating a simple app where I have one html table where I want to show a select query from a database table . From here I found how to send a callback to functions. But my problem is ,
onloadis not calling myshowContact(resultSet) functionHow can I print the
resultSetin the <tbody>tag, I do not want to use javascript concatenation inside theshowContact(resultSet)function.
Part of my index.html file
<table data-role="table" id="table-custom-2" onload="showContact(resultSet)" >
<thead>
<tr class="ui-bar-d">
<th>Id</th>
<th>Name</th>
<th>Number</th>
</tr>
</thead>
<tbody>
<!--Print the resultSet data with a for loop here-->
</tbody>
</table>
My javascript code
function showContact(resultSet){
var db= window.openDatabase("appDb","1.0","appDb",200000);
selectRow("SELECT * FROM contact;", function(resultSet) {
console.log(resultSet);
//Don't want to access the <table> from here
});
}
/** Select Row from Table **/
function selectRow(query, callBack){
var result = [];
db.transaction(function (tx) {
tx.executeSql(query, [], function(tx, rs){
for(var i=0; i<rs.rows.length; i++) {
var row = rs.rows.item(i)
result[i] = { id: row['id'],
name: row['name'],
number: row['number']
}
}
callBack(result);
}, errorHandler);
});
}
onload, but usingaddEventListenerinstead. For 2: You'll need to either use a templating solution like @EmanuelRalha suggests, or use the JavaScript DOM API (e.g.,createElement,createTextNodeandinsertAfter) to build up your table rows and append them to yourtbody. The DOM API is a little tricky to get used to, but if you've worked with XML docs in Java, the concepts are similar.showContactto not take a parameter, but rather retrieve it upon invocation. Then you can have this in your script code:document.getElementById('table-custom-2').addEventListener('load', showContact);BTW, since you never showed whereresultSetis coming from, that could be a source of bugs as well. Make sure you know it's initialized when you invokeshowContact