0

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

one

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?

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.