【发布时间】: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