Ionic, ngЗа как да покажа конкретен резултат?

В моето ionic приложение създадох функция getApiData, за да получа данните от моята база данни Django.

Получавам серия, включваща всички данни за всички потребители вътре, как мога да ги покажа само за конкретен потребител??

По-долу са моите json данни, имам потребител като testuser, testproject 1 и т.н.

Ако искам да покажа само данни, които принадлежат на testuser. Как мога да го направя ??

[{"fields": {"date": "2018-04-16", "duration": 10, "fee": "0.06", "status": "Unpaid", "user": "testuser"}, "model": "apidb2.record1", "pk": 1}, {"fields": {"date": "2018-04-16", "duration": 30, "fee": "0.20", "status": "Unpaid", "user": "testuser"}, "model": "apidb2.record1", "pk": 2}, {"fields": {"date": "2018-04-16", "duration": 35, "fee": "0.23", "status": "Unpaid", "user": "testuser"}, "model": "apidb2.record1", "pk": 3}, {"fields": {"date": "2018-04-16", "duration": 60, "fee": "0.40", "status": "Unpaid", "user": "testuser"}, "model": "apidb2.record1", "pk": 4}, {"fields": {"date": "2018-04-17", "duration": 30, "fee": "0.20", "status": "Unpaid", "user": "testuser"}, "model": "apidb2.record1", "pk": 5}]

Това е моето кодиране за получаване на данни от API..

 getApiData_record() {
    this.restProvider.getApiData_record().then(data =>{
      this.users = data;
      var totalDuration=0;
      var totalFee = 0.00;
      var fee1 = [];

      if (this.users.fields.user = this.pushname){
        for (var i of this.users){
        fee1.push(i.fields.duration);
      }
    }
      for (var j=0; j < fee1.length; j ++){
        console.log("here", fee1[j])
        totalDuration = totalDuration + fee1[j];
      }
      totalFee = totalDuration/60 * 0.80 ;
      this.totalFee = totalFee;
      this.totalDuration = totalDuration;
      console.log("Record Here", data, totalDuration)

    })
  }

Това е html как го показвам.

 <ion-item *ngFor="let user of users">
    <ion-row>
      <ion-col col-4>
        {{user.fields.date}}
      </ion-col>
      <ion-col col-4>
        {{user.fields.duration}} Minute
      </ion-col>
      <ion-col col-4>
        &nbsp; &nbsp; &nbsp; RM {{user.fields.fee}}
      </ion-col>
    </ion-row>
  </ion-item>

Някой знае ли как да покаже само конкретен потребител като testuser only? Получих testuser в глобална променлива като тази

 this.pushname = navParams.get('pushName');

Това pushname е променливата, която искам да покажа. Ако моето pushname, което получих, е testuser, тогава показвам само данни за testuser.

Моля, помогни ми !!! Благодаря, нов съм в йоника, но това ще ми спаси живота. Благодаря. Оценявам !!!!


person Community    schedule 17.04.2018    source източник


Отговори (1)


Добре, след като понякога имам решение за това, просто добавям просто условие if в моя .ts файл..

променям това..

 getApiData_record() {
    this.restProvider.getApiData_record().then(data =>{
      this.users = data;
      var totalDuration=0;
      var totalFee = 0.00;
      var fee1 = [];
  if (this.users.fields.user = this.pushname){
    for (var i of this.users){
    fee1.push(i.fields.duration);
  }
}
  for (var j=0; j < fee1.length; j ++){
    console.log("here", fee1[j])
    totalDuration = totalDuration + fee1[j];
  }
  totalFee = totalDuration/60 * 0.80 ;
  this.totalFee = totalFee;
  this.totalDuration = totalDuration;
  console.log("Record Here", data, totalDuration)

})
}

до това

getApiData_record() {
    this.restProvider.getApiData_record().then(data =>{
      this.users = data;
      var totalDuration=0;
      var totalFee = 0.00;
      var fee1 = [];
      var record1 = [];
      this.newdata = record1;

        for (var i of this.users){
          if(i.fields.user == this.pushname){
            record1.push(i)
            fee1.push(i.fields.duration);
            console.log("123", fee1, i.fields.duration, i.fields.user, this.pushname, this.newdata)
          }
      }
      for (var j=0; j < fee1.length; j ++){
        console.log("here", fee1[j])
        totalDuration = totalDuration + fee1[j];
      }
      totalFee = totalDuration/60 * 0.80 ;
      this.totalFee = totalFee;
      this.totalDuration = totalDuration;
      console.log("Record Here", data, totalDuration)

    })
  }

Просто добавям условие if, ако my fields.user == this.pushname (което е моят ID), тогава го вкарвам в масив с нови данни и ngFor в моя бекенд да покажа тези нови данни, не се колебайте да предоставите друго решение, надявам се това да помогне на други, изправени пред този проблем.

person Community    schedule 17.04.2018