imalik8088
imalik8088

Reputation: 1631

order result of firebase dynamically

I'm new to angular and firebase and I want to order my fetched list of contestants dynamically (3-way-binding).

app: https://shining-torch-1269.firebaseapp.com/#/ see it's not ordered!

code of the service for getting the data from firebase:

app.service('ContestantsService', function ($firebaseArray, FIREBASE_URI) {
    var service = this;
    var ref = new Firebase(FIREBASE_URI);
    var contestants = $firebaseArray(ref);

    service.getContestants = function () {
        return contestants;
    };

    service.addContestant = function (contestant) {
        contestants.$add(contestant);
    };

    service.updateContestant = function (contestant) {
        contestants.$save(contestant);
    };

    service.removeContestant = function (contestant) {
        contestants.$remove(contestant);
    };
});

I have tried already the method var contestants = $firebaseArray(ref).orderBy('score')

Is there a way to order list as to be seen in the link above?

Upvotes: 1

Views: 277

Answers (1)

imalik8088
imalik8088

Reputation: 1631

I get the solution.. here's the solution in code
I basically some parts and here a 4 step solution for the problem.

1 orderByChild

var query = ref.orderByChild('score').limitToLast(10); // added this one
var contestants = $firebaseArray(query);

2 FIREBASE RULES

{
"rules": {
    ".read": true,
    ".write": true,
    ".indexOn":"score",
    "score": {
      ".indexOn": ".value"
    }
}
}

3 angular filter for the reserve

app.filter('reverse', function() {
  return function(items) {
    return items.slice().reverse();
  };
});

4 use the filter in the view

<tr ng-repeat="contestant in main.contestants | reverse ">

Upvotes: 3

Related Questions