Reputation: 429
Basically I need to perform the http request by passing id. Once I get the response, I need to assign the values to the form fields.
I have tried using setValue & patchValue. But didn't work
spService
itemData = new ItemData()
getItembyID(itemID) {
return this.http.get<Object>(this.baseUrl +"items("+itemID+")")
.pipe(map ((response : Response) => {
const data = response['d']
return data
})
).subscribe((data : Response) => {
this.setItemData(data)
})
}
setItemData(data) {
this.itemData.requestno = data.Request_x0020_Number
this.itemData.requestid = data.Request_x0020_ID
edit-form.component.ts
constructor(private spService : SharepointService,
private formBuilder : FormBuilder)
ngOnInit() {
this.route.params.subscribe((param : Params)=> {
const id = param['ID']
this.spService.getItemByID(ID)
})
this.itemData = this.sharepointService.itemData
// In console, I can see the details of the itemData but not able to set to the formcontrols.
this.editForm = this.formBuilder.group({
RequestNo : [''],
RequestId : ['']
})
this.editForm.patchValue({
RequestNo : this.itemData.requestno,
RequestId : this.itemData.requestid
})
} // End of OnInit
edit-form.component.html
<form [formGroup]= "editForm">
<label> DefaultRequestNo </label>
<input type="text" formControlName="RequestNo">
<label> DefaultRequestID </label>
<input type="text" formControlName="RequestId">
</form>
Upvotes: 2
Views: 2930
Reputation: 286
To set the default value on edit click, the can use FormBuilderConfiguration
of @rxweb/reactive-form-validators in which you can set the value coming from edit api call in defaultValue
property like this:
setItemData()
{
let user = new User();
var formBuilderConfig = new FormBuilderConfiguration();
formBuilderConfig.propsConfig = {'RequestNo':{defaultValue:"abc1"},"RequestId":{defaultValue:1}}
this.editForm = <RxFormGroup>this.formBuilder.formGroup(user,formBuilderConfig);
}
You need to import RxReactiveFormsModule
in app component
Here is the complete component code
import { Component, OnInit } from '@angular/core';
import { FormGroup,FormArray } from "@angular/forms"
import { RxFormBuilder,RxFormGroup,FormBuilderConfiguration } from '@rxweb/reactive-form-validators';
import { User, } from './user.model';
@Component({
selector: 'app-user',
templateUrl: './patch-complete.component.html'
})
export class UserComponent implements OnInit {
editForm: RxFormGroup
constructor(
private formBuilder: RxFormBuilder ) { }
ngOnInit() {
let user = new User();
this.editForm = <RxFormGroup>this.formBuilder.formGroup(user);
// this.itemData = this.sharepointService.itemData
// In console, I can see the details of the itemData but not able to set to the formcontrols.
}
setItemData()
{
let user = new User();
var formBuilderConfig = new FormBuilderConfiguration();
formBuilderConfig.propsConfig = {'RequestNo':{defaultValue:"abc1"},"RequestId":{defaultValue:1}}
this.editForm = <RxFormGroup>this.formBuilder.formGroup(user,formBuilderConfig);
}
}
<div>
<form [formGroup]= "editForm">
<label> DefaultRequestNo </label>
<input type="text" formControlName="RequestNo">
<br/>
<label> DefaultRequestID </label>
<input type="text" formControlName="RequestId">
<br/>
<button (click)="setItemData()">SetItemData</button>
</form>
</div>
Here is the Working Example
Upvotes: 1
Reputation: 372
Seems like you are trying to set the defaults manually instead of letting the FormBuilder manage them. You should try modifying your code above to:
this.editForm = this.formBuilder.group({
RequestNo : [data.Request_x0020_Number],
RequestId : [data.Request_x0020_ID]
})
That should work.
Upvotes: 0
Reputation: 29335
the issue here is that your itemData in your service is not available synchronously, but you're accessing it as though it is. You should set up a subject in your service that your component can subscribe to:
modified SP service:
itemData = new ItemData();
private itemDataSource = new ReplaySubject<ItemData>(1);
itemData$ = this.itemDataSource.asObservable();
getItembyID(itemID) {
return this.http.get<Object>(this.baseUrl +"items("+itemID+")")
.pipe(map ((response : Response) => {
const data = response['d']
return data
})
).subscribe((data : Response) => {
this.setItemData(data)
})
}
setItemData(data) {
this.itemData.requestno = data.Request_x0020_Number;
this.itemData.requestid = data.Request_x0020_ID;
this.itemDataSource.next(this.itemData);
}
then you can subscribe in your component:
// just do this once
this.editForm = this.formBuilder.group({
RequestNo : [''],
RequestId : ['']
})
this.sharepointService.itemData$.subscribe(itemData => {
this.itemData = itemData;
this.editForm.setVale({
RequestNo : this.itemData.requestno,
RequestId : this.itemData.requestid
});
});
the reason your logs are "lying" to you; in your service, you set an initial object value and then only ever mutate that object. when you log an object in the console, it doesn't show you the 'point in time' value, it shows you the reference to that object and what values are at that reference, so even though the data isn't actually available when you attempt to set the form values, it still appears in your logs when it does eventually get set.
This is a one of many reasons why you should avoid object mutation in general in Javascript and instead instantiate a new object everytime you change something like:
setItemData(data) {
this.itemData = Object.assign({}, this.itemData, {
requestno: data.Request_x0020_Number,
requestid: data.Request_x0020_ID
});
this.itemDataSource.next(this.itemData);
}
this method will ensure you are not mutating a new object and instead are creating a new object everytime. This particular method may not be right for your use case, but the principle is the same, don't mutate.
Upvotes: 1