在HTML表中显示Sql结果

时间:2011-05-23 09:13:48

标签: javascript html sqlite cordova

我目前正在使用phonegap并尝试通过javascript将sqlite表中的结果显示到html表。我该怎么办才能做到这一点?

    // 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);
  }

}

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

所以结果是celebsDataHandler。如何操作存储在里面的数据以显示在html表中(可能使用for循环或..?)?

1 个答案:

答案 0 :(得分:1)

好的,然后尝试使用此功能:

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);
            }
        }
        }
    }
相关问题