- Home
- Forum
- Angular - EJ 2
- Delete selected row
Delete selected row
Hi, I have a grid with "Add" and "Delete" button in the toolbar as shown below.
<ejs-grid id="Users" #gridUsers (dataBound)='dataBound($event)' [dataSource]="this.AssociatedUsers" height='250' allowSelection="true" [selectionSettings]='selectionOptions' (rowSelected)='rowUserSelected($event)' (rowDeselected)='rowUserDeselected($event)' [toolbar]='toolbar' [editSettings]='editSettings' (actionBegin)="usersActionBegin($event)">
<e-columns><e-column field='UserName' headerText='User Name' textAlign='Center' width='100' isPrimaryKey='true'></e-column> </e-columns></ejs-grid>
Upon "Add", a new user will be created in the database. If the creation succeeds, it will also be added into the grid; otherwise, it will not be added into the grid. This has been achieved by adding the following code into 'usersActionBegin' function and it works as expected.
if (args.requestType === 'save') {
if (!this.isValid) {
args.cancel = true;//prevent the save action to validate the value
const dialog = (args as any).dialog;
this.accountSettingsService.AddUser.subscribe(response => {
let strStatus: string = response.Status;
let description: string = response.Description;
if (strStatus == "Succeed") {
this.isValid = true;//make the flag true once the validation becomes true
this.gridUsers.endEdit();//save data
}
});
}
else {
this.isValid = false;
}
}
Now I want to achieve similar effect on "Delete".
Upon "Delete", the selected user will be deleted from database. If the deletion succeeds, it should also be removed in the grid; otherwise, it will stay in the grid.
However, "Delete" does not work as expected. When a user has been deleted successfully from database, it is still shown in the grid. What did I do wrong?
if (args.requestType === 'delete') {
if (!this.isValid) {
args.cancel = true;
this.accountSettingsService.RemoveUser().subscribe(response => {
let strStatus: string = response.Status;
let description: string = response.Description;
if (strStatus == "Success") {
this.isValid = true;//make the flag true once the validation becomes true
this.gridUsers.endEdit();
}
});
}
else {
this.isValid = false;
}
}
Hi Huifei Rao,
Greetings from Syncfusion support,
We understand that you want to delete a selected user from both the database and the grid upon clicking the "Delete" button in the toolbar. But if the deletion succeeds from the database, the grid will not remove the record. Currently, it seems you are using the endEdit method after successfully deleting the user from the database. However, for the deletion scenario, you can use the deleteRecord method. The deleteRecord method is specifically designed to remove records from the grid, ensuring that the grid's state is accurately updated.
Please refer the below code snippet for more reference,
|
usersActionBegin(args: any): void { if (args.requestType === 'delete' && !this.isDeleting) { this.isDeleting = true; args.cancel = true; const selectedRecords = this.gridUsers.getSelectedRecords(); selectedRecords.forEach((record) => { this.gridUsers.deleteRecord('OrderID', record); //After successfully deleted from database you can use the deleteRecod method to delete the records }); this.isDeleting = false; } } |
Sample: https://stackblitz.com/edit/angular-mrlxzs-rdpngp?file=src%2Fapp.component.html
deleteRecord: https://ej2.syncfusion.com/angular/documentation/api/grid/#deleterecord
Please feel free to get back us for further assistance.
Regards,
Vikram S.
deleteRecord works great. Thanks
Hi Huifei Rao,
Thanks for the update,
We are happy to hear that the provided solution was helpful. Please get back to us if you have faced any difficulties.
Regards,
Vikram S
- 3 Replies
- 2 Participants
- Marked answer
-
HR Huifei Rao
- Jun 25, 2024 03:47 PM UTC
- Jul 3, 2024 09:21 AM UTC