I am trying to accomplish the following with KendoUi Grid widget:
- Display a hierarchical grid - done
- Add a custom toolbar button for adding a detail item to a specific row - done
- When custom button is pressed expand the detail grid and add an item.
This all works up to the point of adding the item (i.e. clicking on the Add Item button expands the detail grid). However if I attempt to add the item in the detailExpand function then the detail grid automatically collapses. I have tried various variations of changing the order of expand\add but nothing seems to work correctly.
<body>
<div id="example" class="k-content">
<div id="grid"></div>
</div>
</body>
$(document).ready(function() {
$("#grid").kendoGrid({
dataSource: {
data: [
{Name: "first", Items: []},
{Name: "second", Items: []}
],
schema: {
model: {
fields: {
Name: { type: "string" }
}
}
},
pageSize: 20
},
height: 430,
scrollable: true,
sortable: true,
filterable: true,
pageable: {
input: true,
numeric: false
},
detailInit: detailInit,
detailExpand: detailExpand,
columns: [
{ field: "Name", title: "Name", width: "130px" },
{ command: [
{name: "edit"},
{name: "destroy"},
{ text: "Add item", click: addItem }],
title: " ", width: "172px" }
],
editable: "inline",
toolbar: ["create"]
});
});
var addItemNow = false;
function addItem(e) {
var row = $(e.currentTarget).closest("tr");
addItemNow = true;
var grid = $("#grid").data("kendoGrid");
grid.expandRow(row);
};
function detailInit(e) {
$("<div id='detailGrid'/>").appendTo(e.detailCell).kendoGrid({
dataSource: {
data: e.data.Items,
schema: {
model: {
fields: {
Description: { type: "string" }
}
}
}
},
columns: [
{ field: "Description", title: "Description", width: "70px" },
{ command: [
{name: "edit"},
{name: "destroy"}
]}
]});
}
function detailExpand(e) {
var grid = e.detailRow.find("#detailGrid").data("kendoGrid");
if(addItemNow)
{
addItemNow = false;
grid.dataSource.add({Description: ""});
}
}
Here is a jfiddle showing this jfiddle