简体   繁体   中英

Ionic (Angular) getting right data from array

I have array of data which includes array inside each item of it, so far i can show the exact data as I want except for id's.

Logic

  1. Show arrays in array
  2. Get arrays id (not arrays in array id explanation in screenshot )

Screenshot

Information provided in image of what ID I need to have

一

Code

Controller

products: any[] = [];
limit = 10;
loading: any;

ngOnInit() {
  this.getProducts();
}

async getProducts() {
    this.loading = await this.loadingController.create({
      message: 'Please wait...',
      spinner: 'dots',
      duration: 3000
    });

    await this.loading.present();

    this.wishlistService.getProducts().subscribe((res) => {
      console.log('res', res);
      console.log('wishes', res['wishes']); // in screenshot you see this data
      for (let wish of res['wishes']) {
        this.products.push(wish.product);
      }
      this.hideLoading();
    });
  }

  private hideLoading() {
    this.loading.dismiss();
  }

// For this function I need the upper ID (currently getting product ID I need wishlist item ID)
removeWishlist(id: string) {
    console.log('product id for remove', id);
    this.wishlistService.remove(id).subscribe(
      data => {
        this.alertService.presentToast(data['message']);
      },
      error => {
        this.alertService.presentToast(error['message']);
      }
    );
  }

View

<ion-row *ngIf="products.length>0 else noItem">
  <ion-col size="6" class="prodCard" size-sm *ngFor="let product of products | slice:0:limit">
    <img [src]="product.photo">
    <h4 class="productTitle">
    {{product.name}}
    </h4>

    <ion-row>
    <ion-col size="9" size-sm>
        Prices here
    </ion-col>
    <ion-col size="3" size-sm class="ion-text-center">
        <ion-icon (click)="removeWishlist(product.id)" color="danger" name="heart"></ion-icon>
    </ion-col>
    </ion-row>
  </ion-col>

</ion-row>

Any idea?

In your display iterable you can add a property for ease of running code. Though I would suggest you to revisit your code, better if you can change in API. if you cant hope this helps

products: any[] = [];
limit = 10;
loading: any;

ngOnInit() {
  this.getProducts();
}

async getProducts() {
    this.loading = await this.loadingController.create({
      message: 'Please wait...',
      spinner: 'dots',
      duration: 3000
    });

    await this.loading.present();

    this.wishlistService.getProducts().subscribe((res) => {
      console.log('res', res);
      console.log('wishes', res['wishes']); // in screenshot you see this data
      for (let wish of res['wishes']) {
        wish.product.iconId = wish.id; // <------ added this
        this.products.push(wish.product);
      }
      this.hideLoading();
    });
  }

  private hideLoading() {
    this.loading.dismiss();
  }

// For this function I need the upper ID (currently getting product ID I need wishlist item ID)
removeWishlist(id: string) {
    console.log('product id for remove', id);
    this.wishlistService.remove(id).subscribe(
      data => {
        this.alertService.presentToast(data['message']);
      },
      error => {
        this.alertService.presentToast(error['message']);
      }
    );
  }

In your html bind it iconId

<ion-row *ngIf="products.length>0 else noItem">
  <ion-col size="6" class="prodCard" size-sm *ngFor="let product of products | slice:0:limit">
    <img [src]="product.photo">
    <h4 class="productTitle">
    {{product.name}}
    </h4>

    <ion-row>
    <ion-col size="9" size-sm>
        Prices here
    </ion-col>
    <ion-col size="3" size-sm class="ion-text-center">
        <ion-icon (click)="removeWishlist(product.iconId)" color="danger" name="heart"></ion-icon>
    </ion-col>
    </ion-row>
  </ion-col>

</ion-row>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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