I want filter form array by typing text in the textbox will filter data in the formarray and return matching row.
Here's a link Here is the stackblitz link I have tried to achieve. I have thousands of form array element and I want to filter it and select the corresponding value from the dropdown and click on update button will update all records.
@Component({
selector: 'app-form-array',
templateUrl: './form-array.component.html',
styleUrls: ['./form-array.component.css']
})
export class FormArrayComponent implements OnInit {
form: FormGroup;
searchText: String = '';
devices: Array<any> = [];
datasets: Array<any> = [];
constructor(private fb: FormBuilder) {
this.form = this.fb.group({
published: true,
credentials: this.fb.array([]),
});
}
ngOnInit() {
this.devices = [
{ name: 'device1' },
{ name: 'device2' },
{ name: 'device3' },
{ name: 'device4' },
{ name: 'device5' },
{ name: 'device6' }
];
this.datasets = [
{ name: 'dataset1' },
{ name: 'dataset2' },
{ name: 'dataset3' },
{ name: 'dataset4' },
{ name: 'dataset5' },
{ name: 'dataset6' }
];
this.devices.forEach((device) => {
this.addCreds();
});
}
addCreds() {
const creds = this.form.controls.credentials as FormArray;
creds.push(this.fb.group({
dataset_name: ['', []],
device_id: ['', []],
}));
this.devices.push({ name: 'device'+ (this.devices.length+1) });
}
submit() {
console.log(this.form.value.credentials);
}
}
<form [formGroup]="form" *ngIf="form">
<input type="checkbox" formControlName="published"> Published
<div *ngIf="form.controls.published.value">
<h2>Credentials</h2>
<button (click)="addCreds()">Add</button>
<button (click)="submit()">submit</button>
<br><br>
<input placeholder="search device" [(ngModel)]="searchText" [ngModelOptions]="{standalone: true}">
<br><br>
<div formArrayName="credentials" *ngFor="let creds of form.controls.credentials?.value | formArrayFilterPipe: searchText; let i = index">
<ng-container [formGroupName]="i">
<table>
<tbody>
<tr>
<td>
{{devices[i]?.name}}
</td>
<td>
<select id="{{datasets[i]?.name + 'choice'}}" formControlName="dataset_name">
<option *ngFor="let dataset of datasets">{{dataset?.name}} </option>
</select>
</td>
</tr>
</tbody>
</table>
</ng-container>
</div>
</div>
</form>```

{{form.controls.credentials?.value | json}}outside<ng-container>. It is showing blank values.