0

I have a dynamics ng-select and input through an arrayForm.

HTML:

<ng-container formArrayName="bottles">
      <div class="col">
        <div class="productContainer">
          <ng-container *ngFor="let bottle of getBottles.controls; let i=index">
            <div [formGroup]="bottle" style="display: flex">
              <label for="alias-{{ i }}">Produit:</label>
              <ng-select id="productInput" (change)='selectTest($event, i)'
                         [items]="test[0] | keyvalue" bindLabel="value.name" bindValue="value.quantity" formControlName="product"></ng-select>
              <label for="bottleQuantity">Bottles needed: </label>
              <input *ngFor="let bott of bottles" style="height: 26px" id="bottleQuantity" type="bottleQuantity" formControlName="bottleQuantity"/><div for="alias-{{ i }}" style="color: green" *ngIf="itemSelected">(Qty: {{itemSelected}})</div>
              <span class="material-icons" style="cursor:pointer; color: red" *ngIf="getBottles.controls.length > 1" type="button addClass" (click)="removeBottle(i)">delete</span>
            </div>
          </ng-container>
          <span class="material-icons" style="cursor:pointer; color: green" (click)="addBottlesInput()">add_circle</span>
        </div>
      </div>
    </ng-container>

What i'd like is to display the own quantity of bottles for each dynamic field using index. But i don't know how to perform this using index (or an easier way ? )

<div for="alias-{{ i }}" style="color: green" *ngIf="itemSelected">(Qty: {{itemSelected}})</div>

See an example here StackBliz

Thank you for help :)

3
  • your index is always 1. Check and correct your structure. Seems like a design problem Commented Apr 15, 2022 at 16:47
  • 1
    What do you want Qty to show? The total available number of bottles for the product? That is, if you select bottle1, then Qty should show 5? Commented Apr 15, 2022 at 18:20
  • I only want the « quantity » for each selected elements Commented Apr 15, 2022 at 21:59

1 Answer 1

1

As you want to show the quantity for the selected product per row:

  1. Need an array to store the selected product.
public itemsSelected: any[] = [];
  1. When a new row is added, you need to add the empty object into itemsSelected array.
public addBottlesInput = () => {
  ...

  this.itemsSelected.push({});
};
  1. When selecting a product for each row, you need to update the object in itemsSelected by index.
public selectTest = (event, i) => {
  if (event) {
    event.index = i;
    console.log('SELECTED', event);
    this.itemsSelected[i] = event.value;
  } else {
    this.itemsSelected[i] = {};
  }
};
  1. Lastly, in HTML, you need to get the object from itemsSelected by index. Use ?. (optional chaining) to safe get the quantity, especially for the case when the product is deselected.
<div style="color: green">
    (Qty: {{ itemsSelected[i]?.quantity }})
</div>

Or

<div style="color: green" *ngIf="itemsSelected[i]">
    (Qty: {{ itemsSelected[i].quantity }})
</div>

Sample Demo on StackBlitz

Sign up to request clarification or add additional context in comments.

1 Comment

Works fine, that’s i what i wanted to do, thk you a lot ! :)

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.