Firebase WEB,按小孩记录日期,月份,年份 [英] Firebase WEB , Group By child records by Date ,Month ,Year

查看:206
本文介绍了Firebase WEB,按小孩记录日期,月份,年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有节点消息,它将有多个带有时间戳的记录。我需要按照日期,月份,年份的记录进行分组,如下所示:

  2016年7月12日
7月13日, 2016
2016年7月14日
.....

例如

  {
messages:{
233110:{
- KMU3Tfp7Sk8DzT0Z_lu:{
.priority:1468326341669,
message:hi,
timestamp:1468326341669
},
-KMU3_u- x4RAWXw78MHe:{
.priority:1468326371206,
message:yes,
timestamp:1468326371206
},
}

我想显示这些信息

  2016年7月12日
您好

....

2016年7月13日
您好

....

我的代码:

  var temp_array = new Array(); 
var m_array = new Array();
chat.messageRef.child(roomId).on('child_added',function(snapshot,prevChildKey){
var msg = snapshot.val();
var d = new Date(msg .timestamp);
var month = d.getDate()+''+ chat.monthNames [d.getMonth()] +,+ d.getFullYear();
m_array.push(msg );
temp_array [month] = m_array;
});
console.log(temp_array);


解决方案

您的问题存在一些问题。

  ref.on('child_added',function(snapshot){
var msg = snapshot.val();
var date = new Date(msg.timestamp);
var id ='date _'+ date.toISOString()。substring(0,10); / / eg'day_2016-07-13'
//从DOM获取或创建此日期的li
var li = document.getElementById(id);
if(!li){
li = document.createElement('li');
li.id = id;
li.innerHTML ='< b> + date.toDateString()+'< b>< br>';
ul.appendChild(li);
}
li.innerHTML = li.innerHTML + msg.message +'< br>';
});

对于我们收到的每个孩子,我们从快照中获取日期并将其转换为所谓的 ISO字符串。前10个字符是日期,例如2016年7月13日。这是日期的一个很好的唯一标识符,所以我们用它在HTML中查找/创建 li 元素。



一旦我们找到或者创建了我们的 li 元素,我们可以安全地添加我们的消息给它。最后一行是这样的。



在jsbin中工作示例: http://jsbin.com/lexaciy/edit?html,js,output


I have node 'messages' , and it will have multiple records with timestamp. I need to group by records by date,month, year as like below

12 July, 2016
13 July, 2016
14 July, 2016
.....

My records look like

{
  "messages" : {
    "233110" : {
      "-KMU3Tfp7Sk8DzT0Z_lu" : {
        ".priority" : 1468326341669,
        "message" : "hi",
        "timestamp" : 1468326341669
      },
      "-KMU3_u-x4RAWXw78MHe" : {
        ".priority" : 1468326371206,
        "message" : "yes",
        "timestamp" : 1468326371206
      },
}      

I would like to display these message

12 July, 2016
    Hi
    Yes
    ....

13 July, 2016
    Hi
    Yes
    ....

My code :

var temp_array = new Array();
var m_array = new Array();
chat.messageRef.child(roomId).on('child_added', function(snapshot,prevChildKey) { 
    var msg    = snapshot.val();
    var d      = new Date(msg.timestamp);
    var month  = d.getDate() +' '+chat.monthNames[d.getMonth()] + ", " + d.getFullYear();
    m_array.push(msg);
    temp_array[month] = m_array;
});    
console.log(temp_array); 

解决方案

There are a few problems in your question. I will focus on grouping the items by date.

ref.on('child_added', function(snapshot) {
  var msg    = snapshot.val();
  var date = new Date(msg.timestamp);
  var id = 'date_'+ date.toISOString().substring(0,10); // e.g. 'day_2016-07-13'
  // get or create the li for this date from the DOM
  var li = document.getElementById(id);
  if (!li) {
    li = document.createElement('li');
    li.id = id;
    li.innerHTML = '<b>' + date.toDateString() + '</b><br>';
    ul.appendChild(li);
  }
  li.innerHTML = li.innerHTML + msg.message + '<br>';
});

For each child we receive, we grab the date from the snapshot and convert it into a so-called ISO String. The first 10 characters of that are the date, e.g. '2016-07-13'. This is a great unique identifier for the date, so we use that to find/create an li element in the HTML.

Once we've found-or-created our li element, we can safely add our message to it. The last line does that.

Working sample in a jsbin: http://jsbin.com/lexaciy/edit?html,js,output

这篇关于Firebase WEB,按小孩记录日期,月份,年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆