当我单击编辑按钮时,我试图获取特定行的用户详细信息并将其填充到表单中,但我不知道该怎么做。我有一个方法,当我单击一行时,我将获取特定 id 的更新表单,但该表单为空。如何获取一个填充了 firstName、lastName、dob 的表单?
export class AddUserComponent implements OnInit {
userConfigRecordForm: FormGroup;
reload: EventEmitter<string> = new EventEmitter();
mode: FormMode = FormMode.NEW;
formMode = FormMode;
isOnViewMode = false;
isExist: boolean = false;
showProgressBar: boolean = false;
showFormContent: boolean = true;
num:number
userRecord: User
existMessage: string = "";
distric=[{
"key":"Colombo",
"value": "Colombo"
},
{
"key":"Gampaha",
"value":"Gampaha"
}
]
constructor(private service:NewUserService,
private snackBar: MatSnackBar,private formBuilder: FormBuilder,private dialogRef: MatDialogRef<AddUserComponent>, private dialog: MatDialog) { }
ngOnInit() {
this.userConfigRecordForm=new FormGroup({
firstName: new FormControl(),
lastName: new FormControl(),
dob: new FormControl()
});
if (this.mode == FormMode.UPDATE) {
this.service.getUserById(this.num).subscribe((data: any) => {
this.userRecord = data;
this.userConfigRecordForm.get("firstName").setValue(data.user.firstName);
this.userConfigRecordForm.get("lastName").setValue(data.user.lastName);
this.userConfigRecordForm.get("dob").setValue(data.user.dob);
});
}
}
save(options) {
this.users.skills=this.selectedValue.toString();
this.users.district=this.selectedDistrict;
this.showProgressBar = true;
this.showFormContent = false;
this.isExist = false;
if (this.mode == FormMode.NEW) {
this.service.addUser({ // method to add user
firstName: this.userConfigRecordForm.get('firstName').value,
lastName: this.userConfigRecordForm.get('lastName').value,
dob: moment(this.userConfigRecordForm.get('dob').value).format('YYYY-MM-DD'),
}).subscribe((data: any) => {
console.log(this.userConfigRecordForm.value);
if (data.status === "userExist") {
this.isExist = true;
this.existMessage = "User is already used !";
this.showProgressBar = false;
this.showFormContent = true;
} else {
if (options == 'exit') {
this.reload.emit();
this.showProgressBar = false;
this.openDialogCreateSucess();
this.dialogRef.close();
} else {
this.showProgressBar = false;
this.showFormContent = true;
this.openDialogCreateSucess();
this.num = data.user.num;
this.mode = FormMode.UPDATE
}
}
}
,
error => {
console.log(error);
this.openDialogFailed();
this.showProgressBar = false;
this.showFormContent = true;
});
}
else {
this.service.updateUser(this.num, //updates user through id
{
num:this.num,
firstName: this.userConfigRecordForm.get('firstName').value, //trying to get it's value to the form
lastName: this.userConfigRecordForm.get('lastName').value,
dob: moment(this.userConfigRecordForm.get('dob').value).format('YYYY-MM-DD'),
}).subscribe((data: any) => {
if (data.status === "userExist") {
this.isExist = true;
this.existMessage = "User is already used !";
this.showProgressBar = false;
this.showFormContent = true;
} else {
if (options == 'exit') {
this.reload.emit();
this.showProgressBar = false;
this.openDialogUpdateSucess();
this.dialogRef.close();
} else {
this.showProgressBar = false;
this.showFormContent = true;
this.openDialogUpdateSucess();
}
}
},
error => {
console.log(error);
this.openDialogFailed();
this.showProgressBar = false;
this.showFormContent = true;
});
getUserById
public getUserById(num){
return this.http.get("http://localhost:8080/dms-training-service/V1/example/users/id/"+ num);
}