javascript React形式:多选

yftpprvb  于 2023-05-27  发布在  Java
关注(0)|答案(1)|浏览(135)

我对React形式非常陌生,所以我不知道如何做我想做的事情。
我有一个Observable,其中我得到以下数据:

AllUsers: [
    {
        User_ID: "xxxA",
        User_Name: "AAAA"
    },
    {
        User_ID: "xxxB",
        User_Name: "BBBB"
    },
    {
        User_ID: "xxxC",
        User_Name: "CCCC"
    }
]

ProductsBoughtBy: [
    {
        Product: {
            Product_ID: "xaxa",
            Product_Name: "Product 1",
            Version: 2
        },
        UsersBought: [
            {
                User_ID: "xxxA",
                User_Name: "AAAA"
            },
            {
                User_ID: "xxxB",
                User_Name: "BBBB"
            }
        ]
    },
    {
        Product: {
            Product_ID: "xbxb",
            Product_Name: "Product 2",
            Version: 4
        },
        UsersBought: [
            {
                User_ID: "xxxA",
                User_Name: "AAAA"
            },
            {
                User_ID: "xxxC",
                User_Name: "CCCC"
            }
        ]
    }
]

在html中,我使用ngx-table来显示一个包含3列的表。第一个选项用于“产品名称”,第二个选项用于“产品版本”,第三个选项用于从Angular 材质中进行多选,其中列出了所有用户,并且默认情况下选中ProductsBoughtBy中的用户。
但我不明白如何创建我的?FormsArray?或者我需要FormsGroup。以及如何将其绑定到html mat-select。
先谢谢你了。

dldeef67

dldeef671#

首先,在组件中导入必要的模块

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';

在组件类中创建FormGroup和FormArray

export class YourComponent implements OnInit {
  form: FormGroup;
  usersArray: FormArray;

  constructor(private formBuilder: FormBuilder) {}

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

    this.usersArray = this.formBuilder.array([]);
    this.form.addControl('users', this.usersArray);
  }
}

现在用您的数据填充表单

data$.subscribe(data => {
  const productsArray = this.form.get('products') as FormArray;

  data.ProductsBoughtBy.forEach(product => {
    const productGroup = this.formBuilder.group({
      name: product.Product.Product_Name,
      version: product.Product.Version,
      users: this.formBuilder.array([])
    });

    product.UsersBought.forEach(user => {
      const userGroup = this.formBuilder.group({
        id: user.User_ID,
        name: user.User_Name
      });

      (productGroup.get('users') as FormArray).push(userGroup);
    });

    productsArray.push(productGroup);
  });
});

现在使用formGroup指令和formArrayName指令将表单控件绑定到HTML模板

<form [formGroup]="form">
  <div formArrayName="products">
    <div *ngFor="let product of form.get('products').controls; let i = index" [formGroupName]="i">
      <div>{{ product.get('name').value }}</div>
      <div>{{ product.get('version').value }}</div>
      <mat-form-field appearance="fill">
        <mat-label>Users</mat-label>
        <mat-select [formControlName]="'users'" multiple>
          <mat-option *ngFor="let user of product.get('users').controls; let j = index" [value]="user.get('id').value">
            {{ user.get('name').value }}
          </mat-option>
        </mat-select>
      </mat-form-field>
    </div>
  </div>
</form>

希望对你有帮助

相关问题