简体   繁体   English

在html上显示的Angular4 Firebase FirebaseListObservable控件索引

[英]Angular4 Firebase FirebaseListObservable control index to display on html

I want to create a leaderboard in my application.So I need to read the names and totalscores info from the Firebase Database and display them on browser. 我想在应用程序中创建一个排行榜。因此,我需要从Firebase数据库中读取名称和总得分信息,并将其显示在浏览器上。 My goal is to to display something like this on browser but replace the Unknown with database usernames and topscores of course. 我的目标是在浏览器上显示类似的内容,但是用数据库用户名和topscore代替Unknown。

Leaderboard 排行榜

  • First:Unknown,Topscore:Unknown 首先:未知,最高分数:未知
  • Second:Unknown,Topscore:Unknown 第二:未知,最高分数:未知
  • Third:Unknown,Topscore:Unknown 第三:未知,最高分数:未知
  • Fourth:Unknown,Topscore:Unknown 第四:未知,最高分数:未知
  • Fifth:Unknown,Topscore:Unknown 第五:未知,最高分数:未知
  • My firebase database is: 我的firebase数据库是:

    "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"
        }
      }
    

    With the following code I can get the inverted info from what I want. 使用以下代码,我可以从想要的信息中获取倒排的信息。

    topusers: FirebaseListObservable<any>;
      constructor(db: AngularFireDatabase) {
    
    
            this.topusers = db.list('users', {
            query: {
             orderByChild: "totalscore",
             limitToLast: 10,
            }
            });
       } 
    

    And last using *ngFor I can display the 5 topscores from the fifth to the first. 最后使用* ngFor,我可以显示从第五个到第一个的5个最高分。

    <ul *ngFor="let topuser of topusers | async">
      <li class = "white" > 
          {{ topuser.username | json }}:
          {{ topuser.totalscore | json }}          
      </li>
    </ul>
    

    My question is that is there any way to control the *ngFor so instead to start from the first index of the list to start from the last and end on the first?? 我的问题是,有什么方法可以控制* ngFor,所以要从列表的第一个索引开始,从最后一个索引开始,在第一个索引上结束? Or is there anyway I can control the index of the FirebaseListObservable that I want to display on the browser?? 还是无论如何,我都可以控制要在浏览器中显示的FirebaseListObservable的索引?

    You don't necessarily want to manipulate the order of the ngFor but instead manipulate the data being passed into the ngFor. 您不一定要操纵ngFor的顺序,而要操纵传递到ngFor中的数据。 You can reverse the order of the data returned from the firebase observable with the .map method 您可以使用.map方法反转可观察到的从Firebase返回的数据的顺序。

    topusers: Observable<any>; // use Observable<> instead of FirebaseListObservable<> since .map is an rxjs method that returns the type Observable
    
    constructor(){
        this.topusers = db.list('users', {
            query: {
                orderByChild: "totalscore",
                limitToLast: 10,
            }
        })
        .map(res => res.reverse());
    }
    

    So now the array being passed to the ngFor is reversed. 因此,现在将传递给ngFor的数组反转。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM