1

我需要打印插入我的数据库的收据,最新的收据应该在我的页面顶部。但是当我查询我的数据库时,我将所有数据存储在一个 var 中,它自然会将最早插入的数据放在我的页面顶部,我该如何反转呢?

function querySuccess(tx, results){
    if(results.rows.length == 0)
    {
        $("#ReceiptsList").html("<p>You have no receipts captured yet</p>");
    }
        else
    {
        var s = "";
        for(var i = 0; i<results.rows.length; i++)
        s += "<a href='edit.html?id="+results.rows.item(i).id+"'><strong><font size = 3> " + results.rows.item(i).name +"<font></strong>" + "&nbsp;&nbsp;&nbsp;&nbsp;<font size = 1>Date:" +results.rows.item(i).date +  "<font></a> ";
    }
        //I tried s.reverse(); but didn't work, nothing was showed on the page
        $("#ReceiptsList").html(s);
}
4

2 回答 2

5

反转循环?从最后开始?

for(var i = results.rows.length-1; i>=0; i--)

或者追加字符串的开头,而不是结尾!

或者让服务器首先返回正确的顺序。

于 2012-06-25T20:54:23.003 回答
0
function querySuccess(tx, results){
if(results.rows.length == 0)
{
    $("#ReceiptsList").html("<p>You have no receipts captured yet</p>");
}
    else
{
    var s = "";
    for(var i = results.rows.length-1; i>=0; i--)
    s += "<a href='edit.html?id="+results.rows.item(i).id+"'><strong><font size = 3> " + results.rows.item(i).name +"<font></strong>" + "&nbsp;&nbsp;&nbsp;&nbsp;<font size = 1>Date:" +results.rows.item(i).date +  "<font></a> ";
}

    $("#ReceiptsList").html(s);
}
于 2012-06-25T20:58:07.910 回答