Angular 4 Firebase angularfire 按值对列表进行排序 [英] Angular 4 Firebase angularfire Sorting a list by value

查看:26
本文介绍了Angular 4 Firebase angularfire 按值对列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Angular 4 Firebase 和 AngularFire,我有以下 firebase 数据库

I am using Angular 4 Firebase and AngularFire and i have the following firebase database

"users" : {
    "Test1" : {
      "totalscore" : 50,
      "username" : "test1"
    },
    "Test2" : {
      "totalscore" : 30,
      "username" : "test2"
    },
    "Test3" : {
      "totalscore" : 20,
      "username" : "test1"
    },
    "Test4" : {
      "totalscore" : 10,
      "username" : "test1"
    },
    "Test5" : {
      "totalscore" : 50,
      "username" : "test1"
    },
    "Test6" : {
      "totalscore" : 30,
      "username" : "test2"
    },
    "Test7" : {
      "totalscore" : 20,
      "username" : "test1"
    },
    "Test8" : {
      "totalscore" : 10,
      "username" : "test1"
    }
  }

我阅读了 firebase 和 AngularFire 的文档,我想获取这些数据作为按总分排序的列表.我尝试了这个 AngularFire 链接 但没有任何帮助.我目前得到了这段代码,它工作正常,但它没有对列表进行排序.

I read the documents of firebase and AngularFire and I want to get those data as a sorted list by totalscore.I tried all the possible ways given by this AngularFire link but nothing helped.I currently got this code which works fine but it doesnt sort the list.

import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';

@Component({
  selector: 'app-homefiller',
  templateUrl: './homefiller.component.html',
  styleUrls: ['./homefiller.component.css']
})

export class HomefillerComponent implements OnInit {
  topusers: FirebaseListObservable<any>;
  totalscore;
  list;
  constructor(db: AngularFireDatabase) {


        this.topusers = db.list('users', {
        query: {
         orderByValue: true,
         limitToFirst: 10,
        }
        });
   }

  ngOnInit() {
  }

}

你能帮我按顺序排序我的列表吗

Can you help me with a way to sort my list by

总分:"

或者告诉我是否可以这样做??

or tell me if it is possible to do that?? ?

推荐答案

orderByValue 用于数据结构如下:

"userscores" : {
    "Test1" : 50,
    "Test2" : 30,
    "Test3" : 20
    "Test4" : 10,
}

在上面,您希望根据每个子节点的值对 userscores 上的查询结果进行排序.

In the above you want to order the results of your query on userscores on the value of each child node.

在您的情况下,您要排序的值位于 users 下的子属性 totalscore 中.所以你应该使用 orderByChild:

In your case, the value you want to sort on is in a child property totalscore under users. So you should use orderByChild:

this.topusers = db.list('users', {
    query: {
     orderByChild: "totalscore",
     limitToFirst: 10,
    }
});

这篇关于Angular 4 Firebase angularfire 按值对列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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