本文介绍了如何对Firebase子项中的值进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的firebase中,我有几个事件,每个都有标题和日期字符串:

  {
events :{
-JscIDsctxSa2QmMK4Mv:{
date:2015年6月19日星期五,
title:Event Two
},
-Jswff0o9bWJeDmUoSA9:{
date:2015年6月12日星期五,
title:Event One
},
-JscIs_oMCJ9aT6- JWDg:{
date:2015年8月10日星期一,
title:Event 3
}
}
}

在我的javascript代码中,我检索事件子并将每个标题和日期推送到一个数组,然后将其附加到我的HTML页面并显示内容。



$ p $ var $ ref = new Firebase ( https://demo.firebaseio.com/events);
var build = new Array();
ref.orderByChild(date)。once(value,function(snapshot){
snapshot.forEach(function(data){
var tmp = data.val();
eventMonth = tmp.date.split()[1];
build.push('< h3>'+ tmp.title +'< / h3>< p>日期:' + tmp.date +'< / p>');
});
$(#event-content)。append(build.join(''));

orderByChild 似乎不工作,我该如何订购事件一:2015年6月12日星期五



>第二场活动:2015年6月19日星期五

第三场活动:2015年8月10日,星期一

解决方案

Firebase没有日期类型,因为JSON没有日期类型,所以它不知道这些字符串中是否存储了日期,为了排序,你必须存储一个基元类型代表这些日期,并提供所需的排序顺序作为比较特林或号码。

例如:一个时间戳。给定 date 是一个JS日期对象,在保存时为每个对象添加 sortDate:date.getTime() p>

  {
events:{
-JscIDsctxSa2QmMK4Mv:{
date:
sortDate:1434697200000,
title:Event Two
},
-Jswff0o9bWJeDmUoSA9:{
date:2015年6月12日星期五,
sortDate:1434092400000,
title:Event One
},
-JscIs_oMCJ9aT6- JWDg:{
date:2015年8月10日星期一,
sortDate:1439190000000,
title:Event 3
}


code


然后:

  ref.orderByChild(sortDate)... 


In my firebase I have several events, each with title and date string:

{
    "events": {
        "-JscIDsctxSa2QmMK4Mv": {
            "date": "Friday, June 19, 2015",
            "title": "Event Two"
        },
        "-Jswff0o9bWJeDmUoSA9": {
            "date": "Friday, June 12, 2015",
            "title": "Event One"
        },
        "-JscIs_oMCJ9aT6-JWDg": {
            "date": "Monday, August 10, 2015",
            "title": "Event Three"
        }
    }
}

In my javascript code, I retrieve the events child and push each title and date to an array then append it to my html page and display the content.

var ref = new Firebase("https://demo.firebaseio.com/events");
var build = new Array("");
ref.orderByChild("date").once("value", function(snapshot) {
  snapshot.forEach(function(data) {
    var tmp = data.val();
    eventMonth = tmp.date.split(" ")[1];
    build.push('<h3>'+tmp.title+'</h3><p>Date: '+tmp.date+'</p>');
  });
  $("#event-content").append(build.join(''));

orderByChild doesn't seem to be working, how can I order the events by date so it can look something like below:

Event One: Friday, June 12, 2015

Event Two: Friday, June 19, 2015

Event Three: Monday, August 10, 2015

解决方案

Firebase doesn't have a date type, since JSON doesn't have it. So it has no idea that there're stored dates in those strings. For sorting purposes you have to store a primitive type that represents these dates and gives required order of sorting when compared as a string or number.

For example: a timestamp. Given date is a JS date object, add sortDate: date.getTime() to each object when saving.

{
  "events": {
    "-JscIDsctxSa2QmMK4Mv": {
        "date": "Friday, June 19, 2015",
        "sortDate": 1434697200000,
        "title": "Event Two"
    },
    "-Jswff0o9bWJeDmUoSA9": {
        "date": "Friday, June 12, 2015",
        "sortDate": 1434092400000,
        "title": "Event One"
    },
    "-JscIs_oMCJ9aT6-JWDg": {
        "date": "Monday, August 10, 2015",
        "sortDate": 1439190000000,
        "title": "Event Three"
    }
  }
}

And then:

ref.orderByChild("sortDate")...

这篇关于如何对Firebase子项中的值进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:26