This is a migrated thread and some comments may be shown as answers.

Expand group on group name click

2 Answers 73 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 12 Jun 2015, 06:00 PM

I was looking through the forums and pieced together a few things to expand a group when you click the text, not just the little arrow. Just thought I'd share. 

I'm using a group header template b/c I like to customize the text. So here it is, in your template drop this in.

<groupheadertemplate>

<asp:label id="lblGridHeader" runat="server" Text='<%# Eval("someCol") %>' />
</groupheadertemplate>

 

Throw in this JS

<script type="text/javascript">

function toggleGroups(index) {

var gridClientID = '<%= RadGrid1.ClientID %>';
var grid = $find(gridClientID);
var masterTable = grid.get_masterTableView();
var groupToggles = masterTable._element.getElementsByTagName('INPUT');

if (index != null) {
if (index < groupToggles.length)
masterTable._toggleGroupsExpand(masterTable._element.getElementsByTagName('INPUT')[index], event);
}

else {
for (var i = 0; i < groupToggles.length; i++) {
masterTable._toggleGroupsExpand(masterTable._element.getElementsByTagName('INPUT')[i], event);
}
}
}
     
</script>

 Now, this part is so simple it's stupid. Took me a while before I realized how easy it was. Attach an ItemDataBound event to your grid. The, create a variable that's public for the page and initialize it to 0

MAKE SURE that is for the page, and not defined within the itemdatabound event. 

int _groupIndex = 0; //stored for the group index so we can expand/collapes on group row click

 Now, in the item data bound event, just do this. 

protected void gridCreqCat_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridGroupHeaderItem)
{
GridGroupHeaderItem item = (GridGroupHeaderItem)e.Item;

//this is an asp label b/c we need to render the <span> tag so we have something to click
Label headLbl = (Label)item.FindControl("lblGridHeader");
headLbl.Attributes.Add("onclick", "toggleGroups(" + _groupIndex.ToString() + ");");
_groupIndex++;
}
}

 

 

2 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 17 Jun 2015, 08:42 AM
Hi Michael,

Thank you for posting your solution for custom expanding/collapsing of the groups through the group headers.

As a token of our gratitude for sharing your solution with the community you will find your Telerik Points updated.


Best Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Michael
Top achievements
Rank 1
answered on 18 Jun 2015, 02:34 PM

Update! I found out my solution doesn't work in firefox. Here is some updated JS that works in all browsers.

function toggleGroups(index) {
        var gridClientID = '<%= gridCreqCat.ClientID %>';
        var masterTableView = $find(gridClientID).get_masterTableView();
        var groupHeader = $telerik.getElementsByClassName(masterTableView.get_element(), "rgGroupHeader")[index];
        masterTableView.toggleGroup(groupHeader,
            {
                toggleChildren: false,
                toggleParents: true
            });
 
    }

Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Michael
Top achievements
Rank 1
Share this question
or