【发布时间】:2020-03-25 11:18:48
【问题描述】:
我已将数据存储在 firebase 实时数据库中,如下所示:
{
"orders" : [ null, {
"res_name" : "order test",
"res_phone" : "1234567890",
"timestamp" : 1585123988381
}, {
"res_name" : "tesst restaurant",
"res_phone" : "987654321",
"timestamp" : 1585124001850
}, {
"res_name" : "ppp",
"res_phone" : "9856847758",
"timestamp" : 1585124624718
} ]
}
现在我想按时间戳值降序对数据进行排序。怎么办?
我在下面对数据进行排序,但它不起作用。
firebase.database().ref('orders').orderByChild('timestamp').on('value', function(snapshot) {
var value = snapshot.val();
var htmls = [];
$.each(value, function(index, value){
if(value){
htmls.push('<tr><td>'+ value.res_name +'</td><td>'+ value.res_phone +'</td><td>'+ value.timestamp +'</td></tr>')
lastIndex = index;
});
$('#tblId').html(htmls);
【问题讨论】:
-
到目前为止您发布的代码看起来不错。它有什么问题?提示:问题可能在于您如何处理
snapshot中的数据,因此您需要发布该代码。显示问题的最简单方法是保持处理相同,但随后console.log出现问题,而不是构建/更新 UI。 -
更新了我的问题...现在可以了吗? @FrankvanPuffelen
标签: javascript firebase firebase-realtime-database