actionBegin(args: SaveEventArgs): void {
if (args.requestType === 'beginEdit' || args.requestType === 'add') {
this.submitClicked = false;
this.orderForm = this.createFormGroup(args.rowData);
}
if (args.requestType === 'save') {
this.submitClicked = true;
if (this.orderForm.valid) {
args.data = this.orderForm.value;
} else {
args.cancel = true;
}
}
}
|
createFormGroup(data: IOrderModel): FormGroup {
return new FormGroup({
id: new FormControl(data.id, Validators.required),
name: new FormControl(data.name, Validators.required)
});
}
|
public dataSourceChanged(state: DataSourceChangedEventArgs): void {
if (state.action === 'add') {
this.crudService.addRecord(state).subscribe(() => {this.grid.notify('recordAdded', state); state.endEdit()});
} else if (state.action === 'edit') {
this.crudService.updateRecord(state).subscribe(() => state.endEdit());
} else if (state.requestType === 'delete') {
this.crudService.deleteRecord(state).subscribe(() => {
state.endEdit();
});
}
} |
crudService.ts
/** POST: add a new record to the server using the observable concept */
addRecord(state: DataSourceChangedEventArgs): Observable<Customer> {
return this.http.post<Customer>(this.customersUrl, state.data, httpOptions);
} |
public dataStateChange(state: DataStateChangeEventArgs): void {
this.crudService.execute(state);
} |
crudService.ts
public execute(state: any): void {
this.getAllData().subscribe(x => super.next(x as DataStateChangeEventArgs));
} |
crudService.ts
/** GET all data from the server */
getAllData(): Observable<any[]> {
debugger;
return this.http.get<Customer[]>(this.customersUrl)
.map((response: any) => (<any>{
result: response,
count: response.length
}))
.map((data: any) => data);
} |