gpt4 book ai didi

javascript - 如何使用 javascript 将数据显示为 HTML

转载 作者:行者123 更新时间:2023-11-30 10:23:24 25 4
gpt4 key购买 nike

我在使用 javascript 将数据显示为 HTML 时遇到问题。我创建的代码只显示最新数据而不是全部数据。我在开发中使用 phonegap。这是代码:

 var oldHtml = document.getElementById("favorite-table-id").innerHTML;

for(var i=0; i<courseIdResult.length;i++) {
//var idResult = courseIdResult[i];
db.transaction(
function(tx) {
var query='SELECT Title,Institution_Id,FullTime,EntryScore,Prerequisites FROM Course WHERE Id='+courseIdResult[i];
tx.executeSql(query,[],function(tx,resultSet) {
console.log("Test 0");
var row = resultSet.rows.item(0);
var newHTML = "<tr> <td> <table> <tr> <td width=1% style='white-space:nowrap;font-weight:bold' align='left'>" +row['Title']+"-"+row['Institution_Id'] +
"</td> <td align='right' style='vertical-align:middle;' rowspan=2> <a href='#'><span class='glyphicon glyphicon-star'></span></a> </td></tr> <tr> <td width=1% style='white-space:nowrap'>" +
"Years:"+row['FullTime']+ " ATAR:"+row['EntryScore']+ " Prereq:"+row['Prerequisites']+
"</td> </tr> </table> </td> </tr>";
document.getElementById("favorite-table-id").innerHTML= oldHtml + newHTML;
},errorCB);
},errorCB
);
}

变量 courseIdResult 是我的查询的 ID,我正在使用循环来获取数据。但是,每次循环进行时, document.getElementById("favorite-table-id").innerHTML 的值总是被覆盖。请帮我解决这个问题。

谢谢

最佳答案

var q = "";
for (var i = 0; i < courseIdResult.length; i++) {
q += (q == "" ? "" : ", ") + "?";
}
var query = 'SELECT Title,Institution_Id,FullTime,EntryScore,Prerequisites FROM Course WHERE Id IN (' + q + ')';
var db = CreateDB();
var row = '';
db.transaction(populateDB, errorDB, successDB);
function populateDB(tx) {
tx.executeSql(query, courseIdResult, function (tx, results) {
var len = results.rows.length;
var arrSectionTableName = [];
if (len > 0) {
$("#favorite-table-id").empty();
var newHTML = "";
for (var i = 0; i < len; i++) {
newHTML += "<tr> <td> <table> <tr> <td width=1% style='white-space:nowrap;font-weight:bold' align='left'>" +row['Title']+"-"+row['Institution_Id'] +
"</td> <td align='right' style='vertical-align:middle;' rowspan=2> <a href='#'><span class='glyphicon glyphicon-star'></span></a> </td></tr> <tr> <td width=1% style='white-space:nowrap'>" +
"Years:"+row['FullTime']+ " ATAR:"+row['EntryScore']+ " Prereq:"+row['Prerequisites']+
"</td> </tr> </table> </td> </tr>";
}
$('#"favorite-table-id"').val(newHTML);
//Refresh your control,eg if it is a listview
$('#favorite-table-id').listview('refresh');
}
});
}
function errorDB(err) {
alert("Error processing SQL " + err.message);
}
function successDB() {

}

关于javascript - 如何使用 javascript 将数据显示为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20698779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com