【问题标题】:Select all checkbox reactive forms Angular 8选择所有复选框反应形式Angular 8
【发布时间】:2020-01-11 12:23:50
【问题描述】:

我的复选框表单中有多个复选框数组。当用户单击全选按钮时,我需要选择所有复选框。通过使用 reactive formsFormArray 这是我尝试过的:

Ts 文件

  get formReceivedSummons() {
    return this.form.get('receivedSummons') as FormArray;
  }

  formReceivedSummonsItems(i: number) {
    return (this.formReceivedSummons.controls[i].get('items')) as FormArray;
  }

  constructor(private inquiryStore: InquiryStoreService, private formBuilder: FormBuilder) { }

  ngOnInit() {
    this.form = this.formBuilder.group({
      receivedSummons: this.formBuilder.array([])
    });
    this.getReceivedSummons();
  }

  getReceivedSummons() {
    this.inquiryStore.summons$.subscribe(result => {
      this.receivedSummon = result;
      this.addCheckboxes();
    });
  }

  selectAllCheckbox() {
    this.formReceivedSummons.controls.map(value => value.setValue(true));
  }

  addCheckboxes() {
    this.formReceivedSummons.setValue([]);
    this.receivedSummon.data.items.map(x => {
      const group = this.formBuilder.group({
        header: [this.receivedSummon.header],
        items: this.formBuilder.array([], [minSelectedCheckboxes(1)])
      });
        (group.get('items') as FormArray).push(this.formBuilder.group({
          name: [x.itemNo],
          isChecked: [false, Validators.required]
        }));
      this.formReceivedSummons.push(group);
    });
  }

HTML 文件

    <form [formGroup]="form" (ngSubmit)="submitSelectedCheckboxes()">
      <ng-container formArrayName="receivedSummons" *ngFor="let summon of formReceivedSummons.controls; let i = index">
        <ng-container [formGroup]="summon">
          <ng-container formArrayName="items" *ngFor="let item of formReceivedSummonsItems(i).controls; let j = index">
            <ng-container [formGroup]="item">
              <input type="checkbox" formControlName="isChecked"> {{item.value.name}}
            </ng-container>
          </ng-container>
        </ng-container>
      </ng-container>
      <br>
    </form>
  <button (click)="selectAllCheckbox()">SELECT ALL</button>

我尝试全选时出错:

错误:必须为表单控件提供一个值,名称为:'header'。

我无法弄清楚错误是什么,可以使用一些指导和建议。

【问题讨论】:

  • 请问你为什么有几个formarrays。在我看来,根据您的代码,每个表单数组中只有 one 复选框。为什么不在一个数组中有复选框?
  • 因为我的复选框是基于我得到的对象响应,这里是stackblitz demo..stackblitz.com/edit/…
  • 是的,我们可以看到每个表单数组只有一个复选框,标题也是重复的。你应该重组你的表格。
  • 你能给我看看 stackblitz 吗?

标签: angular checkbox angular-reactive-forms formarray formgroups


【解决方案1】:

您当前的设置没有什么意义,每个复选框都有一个表单数组。您应该将所有复选框粘贴在一个表单数组中。 header formcontrol 也到处重复。而是将您的代码更改为拥有一次header,并在receivedSummons formarray 中添加复选框:

ngOnInit() {
  this.form = this.formBuilder.group({
    header: [''], // outside formarray
    receivedSummons: this.formBuilder.array([])
  });
  this.getReceivedSummons();
}

getReceivedSummons() {
  this.inquiryStore.summons$.subscribe(result => {
    this.receivedSummon = result;
    this.addCheckboxes();
    this.isShowResponse = true;
  });
}

addCheckboxes() {
  this.form.get('header').setValue(this.receivedSummon.header)
  this.receivedSummon.data.items.map(x => {
    this.formReceivedSummons.push(
      this.formBuilder.group({
        name: x.itemNo,
        isChecked: false
      }))
  });
}

然后在模板中进行修改,去掉内部的formarray:

<form [formGroup]="form" (ngSubmit)="submitSelectedCheckboxes()">
  <ng-container formArrayName="receivedSummons" *ngFor="let summon of formReceivedSummons.controls; let i = index">
    <ng-container [formGroupName]="i">
      <input type="checkbox" formControlName="isChecked"> {{summon.value.name}}
    </ng-container>
  </ng-container>
</form>

最后是selectAll 复选框,像你一样使用地图,但访问isChecked 表单控件:

selectAll() {
  this.formReceivedSummons.controls.map(value => value.get('isChecked').setValue(true));
}

Finally your forked STACKBLITZ

【讨论】:

  • 没有意识到我为每个项目都重复了header。如果我只想提交选中的复选框,我将如何实现这一点?通过过滤isChecked 值?
  • 是的,您可以使用filter 获取检查值:)
  • 我正在尝试过滤,但即使我只选中了一个复选框,仍然有 2 个项目..可以建议解决方案吗? stackblitz.com/edit/angular-udvmad?file=src/app/…
  • this.formReceivedSummons.value.filter(x =&gt; x.isChecked === true);
猜你喜欢
  • 2017-08-26
  • 2019-04-30
  • 1970-01-01
  • 2021-03-22
  • 2015-12-14
  • 2023-04-04
  • 2019-07-10
  • 2016-07-03
  • 1970-01-01
相关资源
最近更新 更多