I added a row manually to a hierarchical kendo grid, where the new row is added to the child kendo grid row after the parent row is expanded. It opens up the popup editor and closes immediately.
Here's the Hierarchy template and the parent grid. The onHierarchy1Save occurs when a new Hierarchy1 is added to the database, it expands the row which makes the Hierachy2 grid to show up, get the Hierarchy2 grid and inserts a new row to it. This opens up a popup editor which gets cancelled immediately without adding any new row to it.
@(Html.Kendo().Grid<Heirarchy>()
.Name("Hierarchy1")
.Columns(c =>
{
c.Bound(h => h.Name).Title("Name");
c.Command(command => command.Edit()).Width(90);
})
.Editable(editable =>
{
editable.Mode(GridEditMode.PopUp);
editable.Window(window => window.Title("Edit Details").Width(500));
})
.ToolBar(toolbar => toolbar.Create().Text("Add new hiearchy2"))
.DataSource(dataSource => dataSource.Ajax()
.Model(model => model.Id(div => div.primaryKey1))
.Batch(true)
.Read("GetHierarchies1", "Home")
.Update("UpdateHierachies1", "Home")
.Create("CreateHierarchies1", "Home")
)
.Navigatable()
.Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
.ClientDetailTemplateId("Hierarchy2Template")
.Events(events => events.Save("onHierarchy1Save")))
<script id="Hierarchy2Template" type="text/kendo-tmpl">
@(Html.Kendo().Grid<Hierarchy2>()
.Name("Hierarchy2Grid_#=primaryKey1#")
.Columns(columns =>
{
columns.Bound(h => h.Name).Width(150);
columns.Command(command => command.Edit()).Width(90);
})
.Editable(editable =>
{
editable.Mode(GridEditMode.PopUp);
editable.Window(window => window.Title("Edit Details").Width(500));
})
.ToolBar(toolbar => toolbar.Create().Text("Add new hierarchy2"))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(5)
.Batch(true)
.Model(model => model.Id(h => h.primaryKey2))
.Read(read => read.Action("GetHierarchy2", "Home", new {foreignKey = "#=primaryKey1#"}))
.Update("UpdateHierarchy2", "Home")
.Create("CreateHierarchy2", "Home", new {foreignKey = "#=primaryKey1#"})
)
.Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
.ToClientTemplate())
</script>
function onHierarchy1Save(e) {
e.preventDefault();
this.saveChanges();
if (!e.model.Id) {
if (confirm("Do you want to add a new hierarchy2?")) {
this.expandRow(this.tbody.find("tr.k-master-row").first());
var primaryKey1 = e.model.primaryKey1;
var grid = $('#Hierarchy2Grid_' + primaryKey1).data("kendoGrid");
grid.addRow();
}
}
}
Here's the Hierarchy template and the parent grid. The onHierarchy1Save occurs when a new Hierarchy1 is added to the database, it expands the row which makes the Hierachy2 grid to show up, get the Hierarchy2 grid and inserts a new row to it. This opens up a popup editor which gets cancelled immediately without adding any new row to it.
@(Html.Kendo().Grid<Heirarchy>()
.Name("Hierarchy1")
.Columns(c =>
{
c.Bound(h => h.Name).Title("Name");
c.Command(command => command.Edit()).Width(90);
})
.Editable(editable =>
{
editable.Mode(GridEditMode.PopUp);
editable.Window(window => window.Title("Edit Details").Width(500));
})
.ToolBar(toolbar => toolbar.Create().Text("Add new hiearchy2"))
.DataSource(dataSource => dataSource.Ajax()
.Model(model => model.Id(div => div.primaryKey1))
.Batch(true)
.Read("GetHierarchies1", "Home")
.Update("UpdateHierachies1", "Home")
.Create("CreateHierarchies1", "Home")
)
.Navigatable()
.Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
.ClientDetailTemplateId("Hierarchy2Template")
.Events(events => events.Save("onHierarchy1Save")))
<script id="Hierarchy2Template" type="text/kendo-tmpl">
@(Html.Kendo().Grid<Hierarchy2>()
.Name("Hierarchy2Grid_#=primaryKey1#")
.Columns(columns =>
{
columns.Bound(h => h.Name).Width(150);
columns.Command(command => command.Edit()).Width(90);
})
.Editable(editable =>
{
editable.Mode(GridEditMode.PopUp);
editable.Window(window => window.Title("Edit Details").Width(500));
})
.ToolBar(toolbar => toolbar.Create().Text("Add new hierarchy2"))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(5)
.Batch(true)
.Model(model => model.Id(h => h.primaryKey2))
.Read(read => read.Action("GetHierarchy2", "Home", new {foreignKey = "#=primaryKey1#"}))
.Update("UpdateHierarchy2", "Home")
.Create("CreateHierarchy2", "Home", new {foreignKey = "#=primaryKey1#"})
)
.Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
.ToClientTemplate())
</script>
function onHierarchy1Save(e) {
e.preventDefault();
this.saveChanges();
if (!e.model.Id) {
if (confirm("Do you want to add a new hierarchy2?")) {
this.expandRow(this.tbody.find("tr.k-master-row").first());
var primaryKey1 = e.model.primaryKey1;
var grid = $('#Hierarchy2Grid_' + primaryKey1).data("kendoGrid");
grid.addRow();
}
}
}