gardarvalur
gardarvalur

Reputation: 1585

Expand/Collapse DetailView in Telerik MVC Grid

I´m trying to force javascript to collapse all other rows when expanding one of them, I just want to keep one row expanded at a time. I´m fairly new to javascript so I hope anyone can point me in the right direction.

I looked over this example I found but for some reason my code doesn´t collapse the rows: Expand/Collapse all DetailViews in a grid

Shouldn´t I collapse the index like I´m doing in the javascript?

My Telerik Grid in MVC Razor View:

<div>
@(Html.Telerik().Grid<RentableUnit>()
           .Name("RentableUnits")
           .Columns(columns =>
                        {
                            columns.Bound(e => e.UnitID).Hidden();
                            columns.Bound(e => e.Name).Width(800);
                            columns.Bound(e => e.IsExpanded).Hidden();
                        })
           .ClientEvents(events => events.OnRowDataBound("employees_onRowDataBound")
                                       .OnDetailViewExpand("unit_onDetailViewExpand")
                                       .OnDetailViewCollapse("unit_onDetailViewCollapse")
                                       .OnDetailViewExpand("toggleDetail")
                                       )
           .DetailView(details => details.ClientTemplate(
               //stuff in template
           )
    .DataBinding(dataBinding => dataBinding.Ajax().Select("RentableUnits", "Home"))
    .Sortable()
)
</div>

My Javascript:

    function toggleDetail(e) {
        var grid = $(this).data('tGrid');
        grid.$rows().each(function (index) {
            //TODO: exclude the expanded row
            grid.collapseRow(index); //doesn´t collapse!
    });
}

Upvotes: 1

Views: 2672

Answers (1)

Rustam
Rustam

Reputation: 746

this will do the trick:

function toggleDetail(e){

  var grid = $(this).data('tGrid');
  grid.$rows().not(e.masterRow).each(function(index, row){
    grid.collapseRow(row);
  });
}

Please note you have 2 handlers defined for OnDetailViewExpand event.

Upvotes: 3

Related Questions