簡體   English   中英

在HTML表中顯示Sql結果

[英]Displaying Sql Results in HTML Table

我目前正在使用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循環或..?)?

好的,然后嘗試使用此功能:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM