使用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() {
}