我的组件有2个字段叫做employee(下拉列表)和EmployeeId,我正在动态添加这两个字段值,如下图所示:当我选择特定的员工(前雇员2)进行编辑时,我……
你可以试试这段代码:
addFieldValue() { if(this.newAttribute.employee && this.newAttribute.id){ const index = this.fieldArray.findIndex((item) => this.newAttribute.employee === item.employee); if (index<0) { this.fieldArray.push(this.newAttribute) }else{ this.fieldArray[index] = this.newAttribute } } this.newAttribute = {}; } editFieldValue(index) { this.newAttribute = {...this.fieldArray[index]}; }
的 但 强> 你不能推2员工_1
如果您想要使用相同的名称推送多个员工,您可以尝试:
editedField:number; addFieldValue() { if(this.newAttribute.employee && this.newAttribute.id){ if (this.editedField == undefined) { this.fieldArray.push(this.newAttribute) }else{ this.fieldArray[this.editedField] = this.newAttribute; this.editedField = undefined; } } this.newAttribute = {}; } editFieldValue(index) { this.editedField = index; this.newAttribute = {...this.fieldArray[index]}; }