【发布时间】:2021-11-17 15:49:56
【问题描述】:
在保存所有网格行时,我需要检查特定列字段是否存在重复项
(this.formGroups.get('items') as FormArray).controls.forEach((item) => {
console.log(item.value.attributeDisplayName);
});
当attributeDisplayName 存在重复值时,我需要通过显示警报或类似内容来阻止表单提交。如何在当前的 forEach 循环中检查它。谢谢
我在我的 HTML 标记中使用剑道角度组件,但我没有使用表单标签。下面是列声明的示例
<kendo-grid-column field="attributeDisplayName" title="CUSTOM FIELD LABEL" width="190">
<ng-template kendoGridEditTemplate let-column="column" let-formGroup="formGroup" >
<input ngDefaultControl class="k-textbox" [formControl]="formGroup.get(column.field)">
<div *ngIf="formGroup.get(column.field).errors && (formGroup.get(column.field).dirty || formGroup.get(column.field).touched)">
<span style="color:red" class="k-icon k-i-warning"></span>
<span style="color:red">CUSTOM FIELD LABEL is a required field</span>
</div>
</ng-template>
</kendo-grid-column>
下面是 .ts 文件中的 FormGroup 声明
public formGroup: FormGroup;
public formGroups: FormGroup = new FormGroup({ items: new FormArray([]) });
public createFormGroup = (dataItem) =>
new FormGroup({
atrributeId: new FormControl(dataItem.atrributeId),
objectType: new FormControl(dataItem.objectType, Validators.required),
attributeDisplayName: new FormControl(dataItem.attributeDisplayName, Validators.required),
dataType: new FormControl(dataItem.dataType, Validators.required),
inputValues: new FormControl(dataItem.inputValues, [Validators.required, InputValuesValidator]),
isGridEligible: new FormControl(dataItem.isGridEligible, Validators.required),
isInvoiceEligible: new FormControl(dataItem.isInvoiceEligible, Validators.required),
});
我使用此 FormGroup 的示例函数
public editRows(grid) {
this.isEdited = true;
let currentRow = 0;
let rows: any = grid.data.data;
for (let i = 0; i < rows.length; i++) {
const formGroup = this.createFormGroup(rows[i]);
this.formGroup = formGroup;
(this.formGroups.get('items') as FormArray).push(formGroup);
grid.editRow(currentRow, formGroup, {skipFocus: true});
currentRow++;
}
}
【问题讨论】:
标签: javascript angular foreach