Display posts in descending posted order

2018-12-31 10:13发布

I'm trying to test out Firebase to allow users to post comments using push. I want to display the data I retrieve with the following;

fbl.child('sell').limit(20).on("value", function(fbdata) { 
  // handle data display here
}

The problem is the data is returned in order of oldest to newest - I want it in reversed order. Can Firebase do this?

17条回答
春风洒进眼中
2楼-- · 2018-12-31 10:58

You are searching limitTolast(Int x) .This will give you the last "x" higher elements of your database (they are in ascending order) but they are the "x" higher elements

if you got in your database {10,300,150,240,2,24,220}

this method:

myFirebaseRef.orderByChild("highScore").limitToLast(4)

will retrive you : {150,220,240,300}

查看更多
有味是清欢
3楼-- · 2018-12-31 10:58

I'm using ReactFire for easy Firebase integration.

Basically, it helps me storing the datas into the component state, as an array. Then, all I have to use is the reverse() function (read more)

Here is how I achieve this :

import React, { Component, PropTypes } from 'react';
import ReactMixin from 'react-mixin';
import ReactFireMixin from 'reactfire';

import Firebase from '../../../utils/firebaseUtils'; // Firebase.initializeApp(config);

@ReactMixin.decorate(ReactFireMixin)
export default class Add extends Component {
    constructor(args) {
        super(args);

        this.state = {
            articles: []
        };
    }

    componentWillMount() {
        let ref = Firebase.database().ref('articles').orderByChild('insertDate').limitToLast(10);
        this.bindAsArray(ref, 'articles'); // bind retrieved data to this.state.articles
    }

    render() {
        return (
            <div>
                {
                    this.state.articles.reverse().map(function(article) {
                        return <div>{article.title}</div>
                    })
                }
            </div>
        );
    }
}
查看更多
人气声优
4楼-- · 2018-12-31 10:59

I have a date variable (long) and wanted to keep the newest items on top of the list. So what I did was:

  • Add a new long field 'dateInverse'
  • Add a new method called 'getDateInverse', which just returns: Long.MAX_VALUE - date;
  • Create my query with: .orderByChild("dateInverse")
  • Presto! :p
查看更多
梦寄多情
5楼-- · 2018-12-31 11:01

just use reverse() on the array , suppose if you are storing the values to an array items[] then do a this.items.reverse()

 ref.subscribe(snapshots => {
       this.loading.dismiss();

      this.items = [];
      snapshots.forEach(snapshot => {

        this.items.push(snapshot);

      });
      **this.items.reverse();**
    },
查看更多
梦该遗忘
6楼-- · 2018-12-31 11:02

Someone has pointed out that there are 2 ways to do this:

  1. Manipulate the data client-side
  2. Make a query that will order the data

The easiest way that I have found to do this is to use option 1, but through a LinkedList. I just append each of the objects to the front of the stack. It is flexible enough to still allow the list to be used in a ListView or RecyclerView. This way even though they come in order oldest to newest, you can still view, or retrieve, newest to oldest.

查看更多
登录 后发表回答