How to alert Delete Sucessfully after Destroy of Kendo Grid?
-
21-12-2019 - |
Question
I am using Kendo Grid to display data. Now I know how to ask confirmation of Deletion though kendo grid. Now I want to display alert like Delete Successfully after sucessfull deletion of the record. How can I do it? Here is my kendo grid code.
@(Html.Kendo().Grid<RxConnectEntities.DeleteFileDTO>().Name("deleteList")
.Columns(columns =>
{
columns.Bound(p => p.DeleteFaxID).Hidden(true);
columns.Bound(p => p.FaxName).Width(100).Title("File Name");
columns.Bound(p => p.PerformedDateTime).Width(75).Title("Archive Date").Format("{0:MM/dd/yyyy}");
columns.Command(command => { command.Destroy().Text("Move"); }).Width(50);
columns.Bound(p => p.FilePath).Hidden(true);
})
.Editable(editable => editable.Mode(GridEditMode.InLine).DisplayDeleteConfirmation("Are you sure want to Move this Finance File?"))
.Pageable(p => p.PageSizes(true))
.Scrollable()
.Sortable()
.Selectable(selectable => selectable.Mode(GridSelectionMode.Single).Type(GridSelectionType.Row))
.Events(events => events.Change("onChange"))
.Groupable()
.Filterable(filterable => filterable.Extra(false).Operators(operators => operators.ForString(str => str.Clear().StartsWith("Starts with").Contains("contains").IsEqualTo("Is equal to"))))
.HtmlAttributes(new { style = "height:738px;" })
.DataSource(dataSource => dataSource
.Ajax().ServerOperation(true)
.PageSize(20)
.Model(m => m.Id(p => p.DeleteFileID))
.Read(read => read.Action("GetFileList", "Fax"))
.Destroy(update => update.Action("MoveFileFromArchiveToQueue", "Operation"))
))
Solution
Change to this:
.Events(events => events.Change("onChange").Remove("YourFunctionForAfterDelete")
Then confirm from that...
The alternative being to create a custom command to delete, use ajax and return success/error messages into a dialog or something similar, which is better but take a little more code and reading.
columns.Command(commands =>
{
commands.Edit() data items
commands.Custom("Delete").Click("DeleteByAJAX"); // The "destroy" command removes data items
}).Width(95);
OTHER TIPS
I tried the above method and could not get it to work as needed. I instead subscribed to the onRequestEnd method. I had several alert messages I needed to throw. 1 for successfully adding a record, 1 for successfully deleting a record, and 1 for successfully updating a record.
I populated a ViewBag in the controller based upon the CRUD operation that was occurring.
I then checked for the Viewbag's value in the View and tossed the correct bootstrap alert upon the page reloading...
@if (ViewBag.Alert == "Success")
{
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Record has been successfully added.</strong>
</div>
}
@if (ViewBag.Alert == "Deleted")
{
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Record has been successfully deleted.</strong>
</div>
}
@if (ViewBag.Alert == "Updated")
{
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Record has been successfully updated.</strong>
</div>
}
I was able to make the page reload when the grid was either adding a record, updating a record, or deleting a record by detecting the operation with the following javascript....
<script>
function onRequestEnd(e)
{
var operationType = e.type;
if(operationType == "destroy" || operationType == "create" || operationType == "update")
{
location.reload();
}
}
</script>