简体   繁体   English

存在使用子级的Angularfire2过滤器数据

[英]Angularfire2 filter data using child exists

I have my data like this .When I need to get data from question list I need to check if the current userid (loggedin user) exists in answeredStudents .If exists then that question need to hide from the user. 我喜欢我的数据这样 。当我需要从问题列表,我需要检查当前用户ID(用户的loggedIn)存在于answeredStudents。如果存在,那么这个问题需要从用户隐藏数据。 Otherwise question will be shown in the list. 否则,问题将显示在列表中。

Here how i get questions: 在这里我如何获得问题:

getQuizesSeries()
  {
    let ref = this.afDB.list('questions').snapshotChanges()
    .map(changes =>{
      console.log(changes);
      return changes.map(c=> ({key:c.payload.key,...c.payload.val()}));
    });

    return ref;

  }

In my constructor I called that function like this: 在我的constructor我这样调用该function

  questions:Observable<any[]>;
  quizes:any;

  constructor(public navCtrl: NavController, public afDb:AngularFireDatabase ,public navParams: NavParams,public dataProvider:DataProvider) {
    this.quizes = this.dataProvider.getQuizesSeries();       
  }

and here is my html where its shows data: 这是我的html ,其中显示了数据:

<ion-list *ngFor="let quiz of quizes |async;let i = index;" >
    <ion-item (click)="passkey(quiz.key)">
        <p>{{quiz.name}}</p>
    </ion-item>
</ion-list>

You are going to have to grab the UID of the current student logged in. 您将必须获取当前登录学生的UID。

eg 例如

stduentID = //the id of the logged in user here

Then when you are creating the questions in the ion-list, you will have an ngIf checking the answeredStudents uid with the loggedIn user uid 然后,当您在离子列表中创建问题时,您将看到一个ngIf用登录用户uid检查AnsweredStudents uid

<ion-list *ngFor="let quiz of quizes |async;let i = index;" >
    <ion-item (click)="passkey(quiz.key)" *ngIf="your logic here">
        <p>{{quiz.name}}</p>
    </ion-item>
</ion-list>

I tried to add a simple example with dummy data to solve this task. 我试图添加一个带有虚拟数据的简单示例来解决此任务。 You need to use filter operator to filter out the questions that are already answered by the logged user. 您需要使用filter运算符来过滤出登录用户已经回答的问题。

In the below code I am checking whether the logged in user has already answered this question or not. 在下面的代码中,我正在检查登录用户是否已经回答了这个问题。 If YES then skip this question else add this question to question list. 如果YES则跳过此问题,否则将此问题添加到问题列表。

let questions = [
  {id: 1, desc: "desc1"},
  {id: 2, desc: "desc2"},
  {id: 3, desc: "desc3"}
];

let answeredStudents = [
  {id: 1, answered: [1]},
  {id: 2, answered: [1, 2]}
];

let loggedUserId = 1;

let questionsObs = Rx.Observable.from(questions);

function hasNotAnswered(id, question) {
  let user;
  answeredStudents.forEach(student => {
    if (student.id === id) {
      user = student;
    }
  });
  return !(user.answered.indexOf(question.id) > -1);
}

questionsObs.filter(question => hasNotAnswered(loggedUserId, question))
  .map(question => questionList.push(question));

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

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