离子,ng如何显示具体结果?

时间:2018-04-17 15:17:19

标签: angular ionic-framework

在我的离子应用程序中,我创建了一个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这样的特定用户?我在像这样的全局变量中收到了testuser

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

这个pushname是我想要显示的变量。如果我收到的pushname是testuser,那么我只显示testuser数据。

请帮帮我!!!谢谢,我是离子的新手,但这将拯救我的生命。谢谢。感谢!!!!

1 个答案:

答案 0 :(得分:0)

好的,有时我有一个解决方案,我只是在我的.ts文件中添加一个简单的if条件..

我改变了这个..

 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条件,如果我的fields.user == this.pushname(这是我的ID)然后我将它推入一个新的数据,并且我在后端显示这个新数据,随时提供其他解决方案,希望这有助于其他人面对这个问题。