我们在C#MVC中有一个Kendo网格,该网格绑定到本地数据源。我们可以添加和删除条目,但是只要使用内置的Edit,然后单击Editing中的Cancel按钮,它就会还原网格中的大多数更改。我们正在从传递给我们的视图的模型中加载数据,并具有Javascript函数以在取消时调用grid.cancelChanges();
API方法。我已经浏览了其他几个类似的主题,但是那里提供的答案都没有对我们的设置有所帮助。网格代码为:
Html.Kendo().Grid<ProviderLocationGridDTO>(Model.NewModel.ProviderLocations)
.Name("locationsGrid")
.Columns(cols =>
{
cols.Bound(p => p.ProviderLocationId).Hidden();
cols.Bound(p => p.PendingChangeId).Hidden();
cols.Bound(p => p.ProviderId).Hidden();
cols.Bound(p => p.LocationId).Hidden();
cols.Bound(p => p.IsRemoved).Hidden();
cols.Bound(p => p.IsAdded).Hidden();
cols.Bound(p => p.IsChanged).Hidden();
cols.Bound(p => p.LocationName).Width("35%").Title("Name").EditorTemplateName("LocationNameAutoCompleteForm").Sortable(true)
.Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(false)))
.HtmlAttributes(new { @class = "" }).ClientTemplate
(
@"<span>#: LocationName #</span>"
);
cols.Bound(p => p.DisplayAddress).Title("Address").Width("20%");
cols.Bound(p => p.AllContacts).Title("Contact").Width("20%");
cols.Bound(p => p.PrimaryLocation.PrimaryLocationValue).Width("10%")
.EditorTemplateName("DDL_ProviderPrimaryLocation")
.Title("Primary");
cols.Command(command =>
{
command.Edit().UpdateText("Save").HtmlAttributes(new { @class = !Model.LocationsFormField.IsEditOnLanding ? "k-state-disabled" : "", @style = !Model.LocationsFormField.IsEditOnLanding ? "pointer-events: none;" : "" });
command.Custom("Undo").Click("onUndoLocationRemove").HtmlAttributes(new { @class = !Model.LocationsFormField.IsEditOnLanding ? "k-state-disabled" : "", @style = !Model.LocationsFormField.IsEditOnLanding ? "pointer-events: none;" : "" });
command.Custom("Remove").Click("onLocationRemove").HtmlAttributes(new { @class = !Model.LocationsFormField.IsEditOnLanding ? "k-state-disabled" : "", @style = !Model.LocationsFormField.IsEditOnLanding ? "pointer-events: none;" : "" });
}).Title("Action").Width("15%");
})
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable(pageable => pageable
.Refresh(true))
.Scrollable()
//.HtmlAttributes(new { style = "height:550px;" })
.Events(events =>
{
events.DataBound("onGridDataBound");
events.Edit("onEditGridRow");
events.Save("onSaveGridRow");
events.Cancel("onCancelGridRow");
events.Remove("onLocationRemove");
})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.ServerOperation(false)
.Model(model =>
{
model.Id(p => p.LocationId);
model.Field(p => p.PrimaryLocation).DefaultValue(defaultPrimaryValue);
})
.Create(create => create.Action("AddLocation", "ProviderUpdate").Data("sendAntiForgery"))
.Update(update => update.Action("UpdateLocation", "ProviderUpdate").Data("sendAntiForgery"))
))
和我们用于编辑/取消的javascript函数是:
function onEditGridRow(e) {
e.preventDefault();
if (e.model.ProviderLocationId !== 0) {
var txtLocationName = $(e.container).parent().find("#LocationName");
$(txtLocationName).attr('readonly', 'true');
}
var txtDisplayAddress = $(e.container).parent().find("#DisplayAddress");
$(txtDisplayAddress).attr('readonly', 'true');
var txtAllContacts = $(e.container).parent().find("#AllContacts");
$(txtAllContacts).attr('readonly', 'true');
}
function onCancelGridRow(e) {
e.preventDefault();
var grid = $("#locationsGrid").data('kendoGrid');
var row = grid.tbody.find("tr[data-uid='" + e.model.uid + "']");
$(row).find(".k-grid-Remove").removeClass("hide-imp");
$(row).find(".k-grid-Undo").addClass("hide-imp");
grid.cancelChanges();
}
任何帮助将不胜感激!