2011-05-23 91 views
1

我目前使用phonegap,並試圖通過JavaScript顯示結果從一個SQLite表到HTML表。我該怎麼做才能做到這一點呢?在HTML表中顯示Sql結果

// 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循環或..?)?

+0

我沒有sqlite的工作,但我也許能提供幫助。你的函數返回什麼? – 2011-05-23 09:44:16

+0

我想要一個函數,允許我使用已檢索的數據並以表格形式顯示它。這讓我很困惑,因爲我不知道我能做到這一點。 – 2011-05-23 14:23:55

回答

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