Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo UI Grid Only Expand One Row at a Time

I have a Kendo Grid which I would like to only be able to expand one row at a time for detail editing. What is the simplest way to do this?

@(Html.Kendo().Grid<MyModel>()
   .Name("MyGrid")
   .ClientDetailTemplateId("MyTemplate")
   .Columns(columns =>
   {
       columns.Bound(b => b.Code);
       columns.Bound(b => b.Name);
       columns.Bound(b => b.Description);
       ...
       columns.Command(cmd => { cmd.Edit(); cmd.Destroy(); });
   })
   .ToolBar(toolbar => toolbar.Create())
   .Editable(editable => editable.Mode(GridEditMode.InLine))
   .DataSource(dataSource => dataSource
      .Ajax()
      .Model(model => model.Id(a => a.Id))
      .Create(create => create.Action("Create", "SysMaint", new { id = Model.ProjectId }))
      .Read(read => read.Action("Read", "SysMaint", new { projectId = Model.ProjectId }))
      .Update(update => update.Action("Update", "SysMaint"))
      .Destroy(destroy => destroy.Action("Destroy", "SysMaint"))
   )
)

<script id="MyTemplate" type="text/kendo-tmpl">
    @(Html.Kendo().TabStrip()
       .Name("TabStrip_#=Id#")
       .SelectedIndex(0)
       .Items(items =>
           {
               items.Add().Text("A").LoadContentFrom("MyPartialA", "SysMaint", new { id = "#=Id#" });
               items.Add().Text("B").LoadContentFrom("MyPartialB", "SysMaint", new { id = "#=Id#" });
           })
       .ToClientTemplate()
    )
</script>
like image 871
Trey Gramann Avatar asked Feb 13 '13 03:02

Trey Gramann


3 Answers

Ends up this is really simple. Just add these few lines.

      ...
      .Update(update => update.Action("Update", "SysMaint"))
      .Destroy(destroy => destroy.Action("Destroy", "SysMaint"))
   )
   .Events(events => events.DetailExpand("detailExpand"))
)

<script type="text/javascript">
    var expandedRow;
    function detailExpand(e) {
        // Only one open at a time
        if (expandedRow != null && expandedRow[0] != e.masterRow[0]) {
            var grid = $('#MyGrid').data('kendoGrid');
            grid.collapseRow(expandedRow);
        }
        expandedRow = e.masterRow;
    }
</script>

I hope this helps somebody.

like image 96
Trey Gramann Avatar answered Nov 15 '22 09:11

Trey Gramann


That works except it does not remove the old detail row. Add the bit marked NEW to remove each previously opened detail row.

if (expandedRow != null && expandedRow != e.masterRow[0]) {
    var grid = $('#RequestsGrid').data('kendoGrid');
    grid.collapseRow(expandedRow);
    expandedRow[0].nextElementSibling.remove(); //NEW
}
expandedRow = e.masterRow;
like image 43
Danny Blue Avatar answered Nov 15 '22 08:11

Danny Blue


Building on Trey's answer, this version will work generically for any grid (using @vikasde's suggestion), and will also work when you have nested grids, so that the child grid when invoking the detailExpand, won't collapse its parent grid row as a side effect.

<script type="text/javascript">
    function detailExpand(ev) {
        var expandedRow = $(ev.sender.wrapper).data('expandedRow');
        // Only one open at a time
        if (expandedRow && expandedRow[0] != ev.masterRow[0]) {
            var grid = $(ev.sender.wrapper).data('kendoGrid');
            grid.collapseRow(expandedRow);
        }
        $(ev.sender.wrapper).data('expandedRow', ev.masterRow);
    }
</script>
like image 42
hatchet - done with SOverflow Avatar answered Nov 15 '22 07:11

hatchet - done with SOverflow