I have a leaderboard in my ionic app, where I show the position of each registered user, the usernames and the points that the users earned.
Usernames and points are stored into the database, instead, I define the position printing an index for each user for all the list that contains the users.
I want be able also to show, at the bottom of the leaderboard, the position of the logged user, like in the image:
in the example, if YYY it's logged I want show "USER in position 2"
How can I, with my code, retrieve user position (maybe store it in a variable) and show it on html? it is possible?
rank.ts (the function that create my leaderbord list)
this.itemRef.orderByChild("total_points").on('value',itemSnapshot =>{
this.items = [];
itemSnapshot.forEach( itemSnap => {
this.items.push(itemSnap.val());
return false;
});
return this.items.reverse();
});
rank.html
<ion-content padding>
<ion-card>
<ion-card-header class ="centered" id="header_classifica" >
<div > USER you are in position XXX</div>
</ion-card-header>
<div id="table">
<div class="row header">
<div class="col classifica"> <b class="voci_table_header"> Posizione</b></div>
<div class="col classifica"> <b class="voci_table_header"> Username</b></div>
<div class="col classifica"> <b class="voci_table_header"> Punti</b></div>
</div>
<div class="row" *ngFor="let item of items; let i = index">
<div class="col classifica">{{i + 1}}</div>
<div class="col classifica">{{ item.username }} </div>
<div class="col classifica">{{ item.total_points }} </div>
</div>
</div>
</ion-card>
</ion-content>
I hope that my problem it's clear. Thank you in advance.
EDIT: SOLVED PROBLEM USING THIS CODE
this.itemRef.orderByChild("total_points").on('value',itemSnapshot =>{
this.items = [];
itemSnapshot.forEach( itemSnap => {
this.items.push(itemSnap.val());
return false;
});
var j = 0;
this.items.reverse().forEach(i=>{
j++;
if (new String(i.username).valueOf() == new String(this.username).valueOf()){
console.log("Trovato")
this.position = j;
}
})
return this.items;
});