我面临一个问题,我想过滤formArray控件

public checklistForm!: FormGroup;

 this.checklistForm = this.fb.group({
      checklistItems: this.fb.array([])
 });

// some logic

  public checklistItems(): FormArray {
    return this.checklistForm.get('checklistItems') as FormArray;
  }

// filter some checklistsItems
const newFormArray = this.checklistItems().value.filter(// some condition, may be check of id) //this return an array  

HTML

<div *ngFor="let checkList of checklistItems().controls; let checkListIndex=index"></div>

我不确定如何过滤这checklistItems().controls

推荐答案

我们只需使用过滤器将值存储在临时变量中,我们将使用该变量来运行下面的*ngFor(这是一个工作示例),其中我根据搜索过滤表单控件

如果有任何疑问请告诉我!

import { CommonModule } from '@angular/common';
import { Component, inject } from '@angular/core';
import {
  ReactiveFormsModule,
  FormArray,
  FormGroup,
  FormControl,
  FormBuilder,
} from '@angular/forms';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, ReactiveFormsModule],
  template: `
  Search: <input #input (input)="search(input.value)"/>
  <form [formGroup]="checklistForm">
    <div formArrayName="checklistItems">
      <div *ngFor="let checkList of filteredCheckListItems; let checkListIndex=index" [formGroupName]="checkListIndex">
        {{checkListIndex}}
        <input type="text" [formControl]="checkList.controls.test" id="test" name="test"/>
      </div>
    </div>
  </form>
  {{checklistForm.value | json}}
  `,
})
export class App {
  fb = inject(FormBuilder);
  public checklistForm!: FormGroup;
  filteredCheckListItems: Array<any> = [];

  // some logica

  public checklistItems(): FormArray {
    return this.checklistForm.get('checklistItems') as FormArray;
  }

  ngOnInit() {
    this.checklistForm = this.fb.group({
      checklistItems: this.fb.array([]),
    });
    const checkListItems = this.checklistItems();
    checkListItems.push(this.fb.group({ test: new FormControl('apple') }));
    checkListItems.push(this.fb.group({ test: new FormControl('banana') }));
    checkListItems.push(this.fb.group({ test: new FormControl('grapes') }));

    // store the full unfiltered checklist items in the variable
    this.filteredCheckListItems = this.checklistItems().controls;
  }

  search(searchStr: string) {
    console.log(searchStr);
    this.filteredCheckListItems = (<Array<FormGroup>>(
      this.checklistItems().controls
    )).filter((item: FormGroup) => {
      return item?.controls?.['test']?.value?.includes(searchStr);
    });
  }
}

bootstrapApplication(App);

Stackblitz Demo

Typescript相关问答推荐

具有映射返回类型的通用设置实用程序

Typescript:将内部函数参数推断为子对象键的类型

参数类型undefined不能分配给参数类型字符串|未定义

使某些类型的字段变为可选字段'

泛型类型,引用其具有不同类型的属性之一

如何表示多层深度的泛型类型数组?

是否可以基于对象的S属性值[]约束对象的其他属性值用于类型化的对象数组?

部分更新类型的类型相等

为什么&;(交集)运算符会删除不相交的属性?

从子类集合实例化类,同时保持对Typescript中静态成员的访问

在Mac和Windows上运行的Web应用程序出现这种对齐差异的原因是什么?(ReactNative)

在抽象类属性定义中扩展泛型类型

如何在Angular /字体中访问API响应的子元素?

仅当类型为联合类型时映射属性类型

断言同级为true或抛出错误的Typescript函数

带有过滤键的 Typescript 映射类型

包含多个不同类构造函数的接口的正确类型?

递归地排除/省略两种类型之间的公共属性

如何使用布尔值构建对象 TYPE,但至少有一个必须为 true

在 TypeScript 中实现类型级别深度优先搜索