Reputation: 1059
I need to print out receipts that inserted into my database, and the latest receipt should be on the top of my page. But when I query my database, I store all the data inside a var s, and naturally it will make the data that was inserted earliest on the top of my page, how do I reverse this?
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>" + " <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);
}
Upvotes: 1
Views: 257
Reputation: 2651
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>" + " <font size = 1>Date:" +results.rows.item(i).date + "<font></a> ";
}
$("#ReceiptsList").html(s);
}
Upvotes: 0
Reputation: 207501
Reverse the loop? Start at the end?
for(var i = results.rows.length-1; i>=0; i--)
Or append the beginning of the string, not the end!
Or have the server return the correct order in the first place.
Upvotes: 5