1

I am currently using phonegap and trying to display results from a sqlite table to a html table through javascript. What should I do to make this possible?

    // callback function to retrieve the data from the prefs table
celebsDataHandler=function(transaction, results) {
  // Handle the results 
  var html = "<ul>"; 
  for (var i=0; i<results.rows.length; i++) { 
    var row = results.rows.item(i); 
    html += '<li>'+row['name']+'</li>\n';
  } 
  html +='</ul>';
  alert(html);
}


// load the currently selected icons
loadCelebs = function() {
  try {

    mydb.transaction(
        function(transaction) {
           transaction.executeSql('SELECT * FROM celebs ORDER BY name',[], celebsDataHandler, errorHandler);
        });

  } catch(e) {
    alert(e.message);
  }

}

From http://wiki.phonegap.com/w/page/16494756/Adding-SQL-Database-support-to-your-iPhone-App

so the resultant is celebsDataHandler. How can I manipulate the data stored inside to display in html table(probably using a for loop or ..?)?

2
  • I have not worked in sqlite but i might be able to help. What do your function return? Commented May 23, 2011 at 9:44
  • i want a function that allows me to use the data that has been retrieved and display it in a table form. this is really confusing me because I don't know how I am able to do that. Commented May 23, 2011 at 14:23

1 Answer 1

1

Okay, then try to use this function:

c3.table = function(data,target,className){
    if(typeof target != 'object'){
    return(null);
    }
    var table = document.createElement('table');
    if(typeof className != 'undefined'){
        table.setAttribute('className', className);
    }
    target.appendChild(table);
        var thead = document.createElement('thead');
        var tr = document.createElement('tr');
        thead.appendChild(tr)
        table.appendChild(thead);
        for (key in data[0]){
        if (data[0].hasOwnProperty(key)) {
            var th = document.createElement('th');
            th.innerHTML = key;
            tr.appendChild(th);
        }
        }
        for (row in data){
        var tbody = document.createElement('tbody');
        var tr = document.createElement('tr');
        tbody.appendChild(tr)
        table.appendChild(tbody);
        var currentRow = data[row];
        for (column in currentRow){
            if (currentRow.hasOwnProperty(column)){
            var td = document.createElement('td');
            td.column = column;
            td.innerHTML = currentRow[column];
            tr.appendChild(td);
            }
        }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.