Please let me know what I should do so that each row of the formArray does not change the values of the already selected values in the other rows?
Please note: Adding controls dynamically and getting the values correctly in the filters work. Its only when I change it, it goes and changes the other dropdowns as well. Thank you.
I have a working FormArray for which the controls are dynamically added at the press of a button. The controls have two dropdowns and depending on the first drop down the value of the second dropdown changes.
However when I add the next dynamic control and change the value of the second dropdown, it resets the value of the first already selected values too. I am using material <mat-select>
for these.
My HTML:
<form [formGroup]="allocationForm" autocomplete="off">
<div fxLayoutAlign="start center" id="allocationsDiv" class="container row">
<mat-card-title style="text-align: left;">Models
<button mat-raised-button class="mat-raised-button-custom" style="padding: 3px;"
(click)="onAddCars()">Add Cars</button></mat-card-title>
<ng-container formArrayName="allocationsArray">
<div *ngFor="let ctrl of allocationControls.controls; let i = index" [formGroupName]="i">
<mat-form-field appearance="outline" style="width: 250px;padding: 3px;">
<mat-label>Car Model</mat-label>
<mat-select formControlName="carModel" id="carModel"
(selectionChange)="filterCarModel($event, i)">
<mat-option *ngFor="let models of distinctModels" [value]="models">{{models}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field appearance="outline" style="width: 350px;padding: 3px;">
<mat-label>Car Submodel</mat-label>
<mat-select formControlName="subModel" id="subModel" (selectionChange)="filtersubModel($event)">
<mat-option *ngFor="let subModel of subModels" [value]="subModel.itemValue">
{{subModel.displayValue}}
</mat-option>
</mat-select>
</mat-form-field>
</ng-container>
</div>
</form>
The onAddCars()
method pushes new controls to the allocationsArray
My ts: for the filterCarModel
method
public filterCarModel(e:any,ind:any)
{
let index = <FormArray>this.allocationForm.controls['allocationsArray'];
var carList = Array.from(new Set(this.productsDTO.filter(x => x.productType === e.value).sort().values()));
this.subModels = Array<Productdropdown>();
carList.forEach(productLists => {
const tempItem = new Productdropdown();
tempItem.displayValue = productLists["carDescription"]
tempItem.itemValue = productLists["carCode"]
this.subModels.push(tempItem);
});
}
How do I filter the second control values not to change the already selected values in the first row of the allocationsArray in my code. using angular 7x
the onAddCars()
is something like this..
public onAddCars(){
this.allocationControls.push(this.formBuilder.group({carModel: '',subModel:''}))
}
Can you please let me know what I should do so that each row of the formArray does not change the values of the already selected values in the other rows? any help is appreciated. Please note: Adding controls dynamically and getting the values correctly in the filters work. Its only when I change it, it goes and changes the other dropdowns as well. Thank you.