【问题标题】:Dynamic form array with dynamic options具有动态选项的动态表单数组
【发布时间】:2019-11-24 17:35:29
【问题描述】:

我想让学生能够在动态数量的学校科目中选择多个研讨会

1.- 我有一个配置数组,这个数组有学生为每个科目选择的选项数量

let configarray = {initdate: 2019-07-01, subjectnumber: 4};

在此示例中,学生可以针对每个科目选择 4 个选项。

let b = this.FormGuardar.get("opciontaller") as FormArray; 
for (let i = 0; i < configarray.subjectnumber; i++) {
     let preregistro = this._fb.group({
         preregistroid: [],
         alumnoporcicloid:[],
         tallercurricularid: []
         });
         b.push(preregistro);
         this.arrayopcion.push({ taller: this.tallerselect }); //tallerselect will display
the options of every subject every subject will have a diferent options

另外,当我选择一个选项时,这是我的代码,用于擦除配置中其他选择中的选定选项。

    selectedTaller(id, index) {
    let seleccionados = [];
    let array = this.FormGuardar.get("opciontaller").value;
    for (let a of array) {
        if (a.tallercurricularid) {
            seleccionados.push(a.tallercurricularid);
        }
    }

    let disponibles = this.SelectTaller.filter(function (item) {
        return seleccionados.indexOf(item.tallercurricularid) == -1;
    });

    for (let i = 0; i < this.arrayopcion.length; i++) {
        let data = [...disponibles],
            control = ((this.FormGuardar.controls.opciontaller as FormArray).controls[i] as FormGroup).controls.tallercurricularid as FormControl,
            seleccionado = this.SelectTaller.find(x => x.tallercurricularid == control.value);
        (((this.FormGuardar.controls.opciontaller as FormArray).controls[i] as FormGroup).controls.clasificadorparaescolarid as FormControl).setValue(seleccionado ? seleccionado.clasificadorparaescolaresid.clasificadorparaescolaresid : null);
        seleccionado ? data.push(seleccionado) : null;
        this.arrayopcion[i].taller = data;
        seleccionado ? control.setValue(seleccionado.tallercurricularid) : null;
    }
}

这里的问题是,如何让这段代码适用于动态数量的主题,并为每个研讨会提供动态选项?

【问题讨论】:

  • 啊!这些名字刺痛了我的眼睛。您是否可以创建一个工作示例 stackblitz 来复制您的问题?另外,请考虑使用 lowerCamelCase 来命名您的类属性和方法。在这一点上,真的很难理解你的代码在做什么。

标签: angular typescript angular-reactive-forms formarray


【解决方案1】:

要删除选项,请参阅stackoverflow question

如果您选择即时创建列表,您可以制作类似see stackblitz的内容

你有一个递归函数,它接受一个索引、一个控件数组和一个带有选项的数组作为参数 - 在这种情况下,我在语言列表之间进行选择 -

getLang(i, array, languageList) {
    return i == 0 ? languageList :
      this.getLang(i - 1, array, languageList.filter(x => x.name != array[i - 1].value))
  }

如果你有类似的功能

createStudent() {
  const auxiliar = new Array(this.configarray.subjectNumber).fill('')
    return new FormGroup(
      {
        name: new FormControl(),
        options: new FormArray(auxiliar.map(x => new FormControl()))
      }
    )
  }

你可以在 ngOnInit 中有一些类似的东西

 this.formArray=new FormArray([]);
 this.formArray.push(this.createStudent())
 this.formArray.push(this.createStudent())

我们的形式是这样的

<form *ngIf="formArray" [formGroup]="formArray">
    <div *ngFor="let form of formArray.controls">
        <div [formGroup]="form">
            <input formControlName="name">
            <div formArrayName="options">
              <div *ngFor="let a of form.get('options').controls;let i=index" >
                 <select  [formControlName]="i">
                    <option value="null" disabled>Select Language</option>
                    <option *ngFor="let lang of 
                       getLang(i,form.get('options').controls,languageList)"
                       [value]="lang.name" >{{lang.name}}</option>
                </select>
           </div>
        </div>
    </div>
    <hr/>
  </div>
</form>

更新我们需要检查不可能选择相同的语言,所以我们订阅了数组选项的更改。所以,如果我们选择例如德语、多拉基语、西班牙语和法语,用西班牙语替换德语后,第3个选项为空

this.formArray.controls.forEach(form => {
      form.get('options').valueChanges.subscribe(res => {
        res.forEach((x, index) => {
          if (index > 0 && res.slice(0, index).find(a=>a==x))
            (form.get('options') as FormArray).at(index).setValue(null, { emit: false })
        })
      })
    })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2018-07-30
    • 2011-09-22
    • 2015-01-04
    • 1970-01-01
    • 2018-07-18
    • 2016-07-19
    相关资源
    最近更新 更多