|
@Component({
selector: 'app-container',
template: `<ejs-grid [dataSource]='data | async' [editSettings]='editSettings' (dataStateChange)= 'dataStateChange($event)' [toolbar]='toolbar' (dataSourceChanged)= 'dataSourceChanged($event)'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign='Right' isPrimaryKey='true' [validationRules]='orderidrules'></e-column>
. . . . . .
</e-columns>
</ejs-grid>`
})
export class AppComponent {
public data: Observable<DataSourceChangedEventArgs>;
constructor(private service: OrdersService) {
this.data = service;
}
public dataStateChange(state: DataStateChangeEventArgs): void {
this.service.execute(state); // for binding the data.
}
public dataSourceChanged(state: DataSourceChangedEventArgs): void {
if (state.action == 'add') {
this.service.add(state).subscribe(() => state.endEdit()); // you can perform add operation
}
else if (state.action === 'edit') {
this.service.edit(state).subscribe(() => state.endEdit()); // you can perform edit operation
}
else if (state.requestType === 'delete') {
this.service.delete(state).subscribe(() => state.endEdit()); // you can perform delete operation
}
}
public ngOnInit(): void {
this.editSettings = { allowEditing: true, allowAdding: true, allowDeleting: true };
this.service.execute(state);
}
} |
|
@Component({
selector: 'app-container',
template: `
<ejs-grid [dataSource]='data | async' [editSettings]='editSettings' [toolbar]='toolbar' (dataSourceChanged)='dataSourceChanged($event)' (dataStateChange)= 'dataStateChange($event)'>
<e-columns>
<e-column field='id' headerText='Customer ID' isPrimaryKey='true'></e-column>
<e-column field= "name" headerText="Customer Name" width="150"></e-column>
</e-columns>
</ejs-grid>`
})
export class AppComponent {
public dataStateChange(state: DataStateChangeEventArgs): void {
this.crudService.execute(state);
}
public dataSourceChanged(state: DataSourceChangedEventArgs): void {
if (state.action === 'add') {
this.crudService.addRecord(state).subscribe(() => 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());
}
}
public ngOnInit(): void {
. . . . . . .
let state = { skip: 0, take: 12 };
this.crudService.execute(state);
}
} |
|
constructor(
private http: HttpClient) {
super();
}
public execute(state: any): void {
this.getAllData().subscribe(x => super.next(x as DataStateChangeEventArgs));
}
addRecord(state: DataSourceChangedEventArgs): Observable<Customer> {
return this.http.post<Customer>(this.customersUrl, state.data, httpOptions);
}
/** DELETE: delete the record from the server */
deleteRecord(state: any): Observable<Customer> {
…
return this.http.delete<Customer>(url, httpOptions);
}
/** PUT: update the record on the server */
updateRecord(state: DataSourceChangedEventArgs): Observable<any> {
return this.http.put(this.customersUrl, state.data, httpOptions);
} |
|
export class DataService extends BehaviorSubject<DataStateChangeEventArgs> {
constructor() {
super(null);
}
public execute(state: any): void {
this.getData(state).subscribe(x => {
super.next(x)
});
}
protected getData(state: DataStateChangeEventArgs): Observable<DataStateChangeEventArgs> {
return Observable.of(<any>{ result: data, count: 2 });
}
} |
Hi Emerson,Thanks for your patience.As per your requirement, we have created a sample and perform CRUD operation using Observable in Grid.In the above sample we have used the InMemoryDbService under “in-mem.ts” file to create a data source and in the “crud.service.ts” file we will be having the WebAPI methods to perform CRUD operations by using HTTPClient. Please refer the below code example and sample for more information.[component.ts]
@Component({selector: 'app-container',template: `(dataSourceChanged) ='dataSourceChanged($event)' (dataStateChange)= 'dataStateChange($event)'> `})export class AppComponent {public dataStateChange(state: DataStateChangeEventArgs): void {this.crudService.execute(state);}public dataSourceChanged(state: DataSourceChangedEventArgs): void {if (state.action === 'add') {this.crudService.addRecord(state).subscribe(() => 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());}}public ngOnInit(): void {. . . . . . .let state = { skip: 0, take: 12 };this.crudService.execute(state);}}[crud.service.ts]
constructor(private http: HttpClient) {super();}public execute(state: any): void {this.getAllData().subscribe(x => super.next(x as DataStateChangeEventArgs));}addRecord(state: DataSourceChangedEventArgs): Observable{ return this.http.post( this.customersUrl, state.data, httpOptions);}/** DELETE: delete the record from the server */deleteRecord(state: any): Observable{ …return this.http.delete(url, httpOptions); }/** PUT: update the record on the server */updateRecord(state: DataSourceChangedEventArgs): Observable{ return this.http.put(this.customersUrl, state.data, httpOptions);}Help documentation :https://ej2.syncfusion.com/16.1.32/angular/documentation/grid/api-gridComponent.html#datastatechangeWe have checked your provided sample, you can use BehaviourSubject instead of Subject in your service might resolve your problem.[DataService]
export class DataService extends BehaviorSubject{ constructor() {super(null);}public execute(state: any): void {this.getData(state).subscribe(x => {super.next(x)});}protected getData(state: DataStateChangeEventArgs): Observable{ return Observable.of({ result: data, count: 2 }); }}Please get back to us if you need further assistance.Regards,R.Dhivya
|
[tsconfig.json]
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": false,
. . . .
}
} |
|
[app.component.html]
<ejs-grid [dataSource]='data | async' [editSettings]='editSettings' [toolbar]='toolbar' (dataSourceChanged)='dataSourceChanged($event)' (dataStateChange)= 'dataStateChange($event)'>
<e-columns>
<e-column field='id' headerText='Customer ID' width='120' textAlign='Right' isPrimaryKey='true'></e-column>
<e-column field= "name" headerText="Customer Name" width="150"></e-column>
</e-columns>
</ejs-grid>
[app.component.ts]
constructor(private crudService: CrudService) {
this.data = crudService;
}
getData(): void {
this.crudService.getAllData()
.subscribe((customers) => {
alert(JSON.stringify(customers))
});
}
public dataStateChange(state: DataStateChangeEventArgs): void {
this.crudService.execute(state);
}
public dataSourceChanged(state: DataSourceChangedEventArgs): void {
if (state.action === 'add') {
this.crudService.addRecord(state).subscribe(() => 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());
}
}
public ngOnInit(): void {
this.editSettings = { allowEditing: true, allowAdding: true, allowDeleting: true };
this.toolbar = ['Add', 'Edit', 'Delete', 'Update', 'Cancel'];
let state = { skip: 0, take: 12 };
this.crudService.execute(state);
} |
Would it possible for someone to provide an example code on how to handle paging, sorting and grouping on the server side. net.Core I'm using batchmode and have the http services wired up client side to my controllers, just not sure how to handle CRUD operations, what do I return. I've tired returning json object with result and count. but grid does not show any data. Thanks
|
app.component.ts
export class AppComponent {
public data: Observable<DataStateChangeEventArgs>;
public pageOptions: Object;
public count = 0;
public printData: Object[];
@ViewChild('grid', {static: true})
public grid: GridComponent;
public state: DataStateChangeEventArgs;
public toolbarOptions: ToolbarItems[];
constructor( public service: OrdersService) {
this.data = service;
}
public dataStateChange(state: DataStateChangeEventArgs): void {
this.service.execute(state);
}
public ngOnInit(): void {
this.pageOptions = { pageSize: 20 };
let state = { skip: 0, take: 20 };
this.service.execute(state);
}
}
|
|
app.component.ts
export class NormalEditComponent implements OnInit {
public data: Observable<DataStateChangeEventArgs>;
public pageOptions: Object;
public editSettings: Object;
public toolbar: string[];
public state: DataStateChangeEventArgs;
customers: Customer[];
@ViewChild('grid')
public grid: GridComponent;
constructor(private crudService: CrudService) {
this.data = crudService;
}
getData(): void {
this.crudService.getAllData()
.subscribe((customers) => {
alert(JSON.stringify(customers));
});
}
public dataStateChange(state: DataStateChangeEventArgs): void {
this.crudService.execute(state);
}
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();
});
} else if (state.requestType === 'batchsave') {
if ((state.changes as any).changedRecords.length > 0) {
this.crudService.batchChangedRecord(state).subscribe(() => {
state.endEdit();
});
}
if ((state.changes as any).addedRecords.length > 0) {
this.crudService.batchAddedRecord(state).subscribe(() => state.endEdit());
}
if ((state.changes as any).deletedRecords.length > 0) {
this.crudService.batchDeletedRecord(state).subscribe(() => state.endEdit());
}
}
}
public ngOnInit(): void {
this.editSettings = { allowEditing: true, allowAdding: true, allowDeleting: true, mode: 'Batch' };
this.toolbar = ['Add', 'Edit', 'Delete', 'Update', 'Cancel'];
const state: any = { skip: 0, take: 12 };
this.crudService.execute(state);
}
}
|