3

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>```






7
  • you want to filter only device name or dataset name also ? Commented Aug 7, 2019 at 10:49
  • No, I want filter only device name Commented Aug 7, 2019 at 10:56
  • If I check this {{form.controls.credentials?.value | json}} outside <ng-container>. It is showing blank values. Commented Aug 7, 2019 at 11:12
  • Can you check once now on stackblitz.com/edit/santosh-angular-form-array Commented Aug 7, 2019 at 12:18
  • 1
    @DpGp I have already provided solution below. can you please check that ? Commented Oct 10, 2019 at 6:26

1 Answer 1

1

check this

form-array-filter-pipe.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'formArrayFilterPipe'
})
export class FormArrayFilterPipe implements PipeTransform {

  transform(items: any[], searchText: string): any[] {
    if(!items) return [];
    if(!searchText) return items;

    searchText = searchText.toLowerCase();
    return items.filter( it => {
      console.log('abc', it.value.device_id);
      return it.value.device_id.toLowerCase().includes(searchText);
    });
  }

}

output enter image description here

update this code and check. Let me know in case of any doubt.

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.