|
@Component({
selector: 'fetchdata',
template: ` <ejs-grid [dataSource]='data' [editSettings]='editSettings' (beforeBatchSave)="batchSave($event)" [toolbar]='toolbar' height='273px'>
<e-columns>
. . .
</e-columns>
</ejs-grid>`,
})
export class FetchDataComponent {
. . .
ngOnInit(): void {
this.editSettings = { allowEditing: true, allowAdding: true, allowDeleting: true, mode: 'Batch' };
this.toolbar = ['Add', 'Edit', 'Delete', 'Update', 'Cancel'];
. . .
}
batchSave(e: any) {
var data = {
action: 'batch', Changed: e.batchChanges.changedRecords, Added: e.batchChanges.addedRecords, Deleted: e.batchChanges.deletedRecords
}
var ajax = new Ajax();
ajax.data = JSON.stringify(data);
ajax.url = "/Home/BatchUpdate";
ajax.type = "POST";
(ajax as any).contentType = "application/json; charset=utf-8";
(ajax as any).successHandler = function (data:any) {
// you can refresh the grid here with new data
this.grid =data;
}
ajax.send();
}
}
|
|
@Component({
selector: 'fetchdata',
template: ` <ejs-grid #grid [dataSource]='data' [editSettings]='editSettings' (beforeBatchSave)="beforeBatchSave($event)" [toolbar]='toolbar' height='273px'>
<e-columns>
. . .
</e-columns>
</ejs-grid>`,
})
export class FetchDataComponent {
. . .
beforeBatchSave(e: any) {
const comp = this;
if (comp.flag) {
e.cancel = true;
let data = {
action: 'batch', Changed: e.batchChanges.changedRecords, Added: e.batchChanges.addedRecords, Deleted: e.batchChanges.deletedRecords
}
var ajax = new Ajax();
ajax.data = JSON.stringify(data);
ajax.url = "/Home/BatchUpdate";
ajax.type = "POST";
(ajax as any).contentType = "application/json; charset=utf-8";
(ajax as any).successHandler = function (data: any) {
comp.flag = false;
// calling the batchSave() method if the Ajax success
(comp.grid.editModule as any).editModule.batchSave();
comp.flag = true;
};
(ajax as any).failureHandler = function (data: any) {
// close the Edit state if the AJAX fails
comp.grid.closeEdit();
};
ajax.send();
}
}
} |
Hi Samir,
You can achieve your requirement by using the following way. In this solution, we have canceled the batch save action initially and if the AJAX request is success, We have re-called the batchSave() method otherwise we called closeEdit() method to discard the changes. Please refer to the below code example, Documentation link and sample link.
[component.ts]
@Component({selector: 'fetchdata',template: ` <ejs-grid #grid [dataSource]='data' [editSettings]='editSettings' (beforeBatchSave)="beforeBatchSave($event)" [toolbar]='toolbar' height='273px'><e-columns>. . .</e-columns></ejs-grid>`,})export class FetchDataComponent {. . .beforeBatchSave(e: any) {const comp = this;if (comp.flag) {e.cancel = true;let data = {action: 'batch', Changed: e.batchChanges.changedRecords, Added: e.batchChanges.addedRecords, Deleted: e.batchChanges.deletedRecords}var ajax = new Ajax();ajax.data = JSON.stringify(data);ajax.url = "/Home/BatchUpdate";ajax.type = "POST";(ajax as any).contentType = "application/json; charset=utf-8";(ajax as any).successHandler = function (data: any) {comp.flag = false;// calling the batchSave() method if the Ajax success(comp.grid.editModule as any).editModule.batchSave();comp.flag = true;};(ajax as any).failureHandler = function (data: any) {// close the Edit state if the AJAX failscomp.grid.closeEdit();};ajax.send();}}}
Regards,Madhu Sudhanan P
|
@Component({
selector: 'fetchdata',
template: ` <ejs-grid #grid [dataSource]='data' [editSettings]='editSettings' (actionBegin)="actionBegin($event)" [toolbar]='toolbar' height='273px'>
<e-columns>
. . .
</e-columns>
</ejs-grid>`,
})
export class FetchDataComponent {
. . .
actionBegin(e: any) {
const comp = this;
if (comp.flag) {
e.cancel = true;
var ajax = new Ajax();
ajax.data = JSON.stringify(e.data);
ajax.url = "/Home/Update";
ajax.type = "POST";
(ajax as any).contentType = "application/json; charset=utf-8";
(ajax as any).successHandler = function (data: any) {
comp.flag = false;
// calling the endEdit() method if the Ajax success
(comp.grid.editModule as any).editModule.endEdit();
comp.flag = true;
};
(ajax as any).failureHandler = function (data: any) {
// close the Edit state if the AJAX fails
comp.grid.closeEdit();
};
ajax.send();
}
}
}
|
Hi Sahal,
Thanks for contacting Syncfusion support.
We can achieve the same behavior using the actionBegin event for the dialog edit mode as follows.
@Component({selector: 'fetchdata',template: ` <ejs-grid #grid [dataSource]='data' [editSettings]='editSettings' (actionBegin)="actionBegin($event)" [toolbar]='toolbar' height='273px'><e-columns>. . .</e-columns></ejs-grid>`,})export class FetchDataComponent {. . .actionBegin(e: any) {const comp = this;if (comp.flag) {e.cancel = true;var ajax = new Ajax();ajax.data = JSON.stringify(e.data);ajax.url = "/Home/Update";ajax.type = "POST";(ajax as any).contentType = "application/json; charset=utf-8";(ajax as any).successHandler = function (data: any) {comp.flag = false;// calling the endEdit() method if the Ajax success(comp.grid.editModule as any).editModule.endEdit();comp.flag = true;};(ajax as any).failureHandler = function (data: any) {// close the Edit state if the AJAX failscomp.grid.closeEdit();};ajax.send();}}}
Regards,Madhu Sudhanan P
Hi Sahal,Query: Although this solution works, the data returned cannot be updated to the grid. Meaning the primarykey id is not updated to the row. The server returns the inserted/ updated row. What is the mechanism to update the grid's specific row with the new data from the serverWe have validated your query and by default the edited data is updated in the grid when you call endEdit method. Could you please share the below details, it will be helpful to provide a better solution.
- Bind actionFailure event to grid and share the error details, if any error throws.
- Share grid code example or sample if it is possible.
- Are you process edited values in server side and the update the processed values in grid?
Regards,Thavasianand S.
|
actionBegin(args: SaveEventArgs): void {
if (args.requestType === 'beginEdit' || args.requestType === 'add') {
this.item = <Item>args.rowData;//Object.assign({}, args.rowData);
}
if (args.requestType === 'save') {
const comp = this;
if (comp.flag) {
args.cancel = true;
if (this.itemForm.valid) {
this.itemService.upsert(this.item).subscribe(i => {
//this is the row data (i) the server returns after inserting it to the database. It will contain the id(primary key ) generated by the server
args.data = i;
comp.flag = false;
if (args.action === 'add') {
comp.grid.editModule.addRecord(args.data, 0);
} else {
(comp.grid.editModule as any).editModule.endEdit();
}
comp.flag = true;
//alert("Sucess")
},
error => {
comp.grid.closeEdit();
comp.flag = true;
alert(error);
})
} else {
args.cancel = true;
}
}
}
}
|