亲爱的 Stackoverflow 用户,
我们创建的数据库如下:
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS Gerechten');
tx.executeSql('CREATE TABLE IF NOT EXISTS Gerechten (id INTEGER PRIMARY KEY AUTOINCREMENT, Cat TEXT NOT NULL, Name TEXT NOT NULL, Desc TEXT NOT NULL)');
tx.executeSql('INSERT INTO Gerechten(Cat,Name,Desc) VALUES ("Voorgerecht", "Tomatensoep", "Heerlijke romige tomatensoep")');
tx.executeSql('INSERT INTO Gerechten(Cat,Name,Desc) VALUES ("Hoofdgerecht", "Pizza", "Kaas en tomaat")');
tx.executeSql('INSERT INTO Gerechten(Cat,Name,Desc) VALUES ("Voorgerecht", "Groentensoep", "Goed gevulde groentensoep")');
tx.executeSql('INSERT INTO Gerechten(Cat,Name,Desc) VALUES ("Voorgerecht", "Kippensoep", "Kippensoep met stukjes kip")');
tx.executeSql('INSERT INTO Gerechten(Cat,Name,Desc) VALUES ("Hoofdgerecht", "Biefstuk", "Biefstuk, wordt geserveerd met patat of rijst")');
}
我有一个Javascript循环的问题,代码如下:
function queryDB(tx){
tx.executeSql('SELECT DISTINCT "Cat" FROM Gerechten',[],function (tx, results) {
var len = results.rows.length, i;
msg = "<p>Found rows: " + len + "</p>";
document.write(msg + '<br/>');
for (i = 0; i < len; i++){
document.write(results.rows.item(i).Cat + '<br/>');
tx.executeSql('SELECT * FROM Gerechten WHERE "Cat" ="'+results.rows.item(i).Cat+'"',[],function (tx, results2) {
var len2 = results2.rows.length, y;
msg = "<p>Found rows: " + len2 + "</p>";
document.write(msg + '<br/>');
for (y = 0; y < len2; y++){
document.write(results2.rows.item(y).Name + '<br/>');
}
}, null);
}
}, null);
}
代码的问题是首先输出类别,然后显示名称(以正确的顺序):
Voorgerecht
Hoofdgerecht
Tomatensoep
Groentensoep
Kippensoep
Pizza
Biefstuk
正确的顺序应该是:
Voorgerecht
Tomatensoep
Groentensoep
Kippensoep
Hoofdgerecht
Pizza
Biefstuk
有谁知道如何解决这个问题?
亲切的问候,
丹尼斯