0

I am running with a problem in which I have a requirement to create dynamic row of inputs on click of Add. I bind my model with element using index and its all working fine.

<div class="row" *ngFor="let field of fieldArray; let i = index">
  <div class="col-sm-2 form-group">
    <label for="grid-input-19">Service Name</label>
      <select class="form-control" name="Service-{{i}}" [(ngModel)]="field.selectedService" #Service="ngModel" (ngModelChange)="onChange($event)">
          <option *ngFor="let service of serviceList" [selected]="service.Service == field.selectedService"
                  [ngValue]="service">
            {{service.Service}}
          </option>
        </select>
      </div>
      <div class="col-sm-4 form-group">
        <div class="form-group">
          <label for="grid-input-18">Service Description</label>
          <textarea rows="1" name="Description-{{i}}" [(ngModel)]="field.selectedService.Description" required
                    class="form-control" [readonly]="true"></textarea>
        </div>
      </div>
      <div class="col-sm-1 form-group">
        <label>Quantity</label>
        <input type="number" min="1" value="1" step="any" name="Quantity-{{i}}" id="daterange-5" [(ngModel)]="field.selectedService.Quantity" required
               maxlength="50" class="form-control" (blur)="onQuantityChange($event.target.value)">
      </div>
      <div class="col-sm-2 form-group">
        <label>Price</label>
        <input type="number" min="0" value="0" step="any" name="Price-{{i}}" id="daterange-5" [(ngModel)]="field.selectedService.Price" required
               maxlength="50" class="form-control" [readonly]="true">
      </div>
    </div>
</div>

Question is on Quantity change I am calling a function which requires to modify prices. But quantity of every row input is changing all together. Suppose there are three rows of input. So on changing Quantity of first row input. All are deflecting together. What am I doing wrong.

onQuantityChange(quantity) {
    // How do I bind the value of row input price which is created dynamically on quantity change
    this.field.Price *= parseInt(quantity);
}

Any help would be appreciated !

4
  • the rows you are talking about, can you post code of table. Commented Apr 6, 2018 at 11:17
  • I already did and it is not a table these are div's created dynamically. There is a problem in binding I think so. Commented Apr 6, 2018 at 11:19
  • Can you share onQuantityChange method signature? Commented Apr 6, 2018 at 11:33
  • @ManojChalode I have added the code where I wants to set the value of price input created dynamically on Quantity change Commented Apr 6, 2018 at 11:42

1 Answer 1

1

Try generating the id for the input quantity & price uniquely using the index value from ngFor like u did for name.

<input type="number" min="1" value="1" step="any" name="Quantity-{{i}}" id="quantity{{i}}">
<input type="number" min="1" value="1" step="any" name="price-{{i}}" id="price{{i}}">

Once you have a unique id, u will be able to recognize quantity & price using the event id.

onQuantityChange(quantity,event) {
    this.fieldID = event.target.id.replace( /^\D+/g, '');  
              // output will be something like 0 for first quantity
              //then   get correcponding price element and multiply
    this.field.Price *= parseInt(quantity);  
}
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.