Firebase Web数据在容器中排序

时间:2017-07-12 16:34:50

标签: javascript sorting firebase firebase-realtime-database

我目前正在开发一个Firebase项目,但我仍然坚持数据排序。 这是我的javascript代码:

function displayContent() {
  if (firstKey != null) {
    var i = 0;
    ref.orderByKey().endAt(firstKey).limitToLast(5).on("child_added", function(childSnapshot) {
      var childType;
      if (i == 0) {
        firstKey = null;
        childType = "lastChild";
      } else {
        childType = "firstChild";
      }
      i++;
      if (i < 5) {
        var containerElement = document.getElementsByClassName('post-container')[0];
        if (childSnapshot.val().image != null) {
          containerElement.insertBefore(
            createPostElementImg(
              childSnapshot.key, 
              childSnapshot.val().title, 
              childSnapshot.val().desc, 
              childSnapshot.val().image),
            containerElement.childType);
          if (firstKey == null) {
            firstKey = childSnapshot.key;
            console.log(firstKey);
          }
        } else {
          containerElement.insertBefore(
            createPostElement(
              childSnapshot.key, 
              childSnapshot.val().title, 
              childSnapshot.val().desc),
            containerElement.childType);
          if (firstKey == null) {
            firstKey = childSnapshot.key;
            console.log(firstKey);
          }
        }
      }
    });
  } else {
    ref.orderByKey().limitToLast(5).on("child_added", function(childSnapshot) {
      var containerElement = document.getElementsByClassName('post-container')[0];
      if (childSnapshot.val().image != null) {
        containerElement.insertBefore(
          createPostElementImg(
            childSnapshot.key, 
            childSnapshot.val().title, 
            childSnapshot.val().desc, 
            childSnapshot.val().image),
          containerElement.firstChild);
        if (firstKey == null) {
          firstKey = childSnapshot.key;
          console.log(firstKey);
        }
      } else {
        containerElement.insertBefore(
          createPostElement(
            childSnapshot.key, 
            childSnapshot.val().title, 
            childSnapshot.val().desc),
          containerElement.firstChild);
        if (firstKey == null) {
          firstKey = childSnapshot.key;
          console.log(firstKey);
        }
      }
    });
  }
}

当firstKey等于null时,第一次调用被正确排序,但是加载onScroll的数据(当firstKey不为null时)以错误的顺序显示。有没有办法对数据进行排序? 谢谢!

修改 我有这段代码用于检测正在滚动到底部的页面:

$(document).ready(function() {
var l_post = 0;
var n_post = 0;
var ref = firebase.database().ref().child('Blog');
ref.once("value", function(snapshot) {
n_post = snapshot.numChildren();
console.log(n_post);
});
jQuery(
function($)
{
  $(".content").bind('scroll', function()
                            {
                              if($(this).scrollTop() + $(this).innerHeight()==$(this)[0].scrollHeight)
                              {
                                if(l_post < n_post) {
                                  displayContent();
                                  l_post = l_post + 5;
                                  console.log(l_post);
                                }
                              }
                            })
}
);
});

0 个答案:

没有答案