Thursday, September 12, 2013

SharePoint : SPGridView - Collapse groups by default

If using SPGridView and groups need to be collapsed by default when the page is loaded, place a script editor webpart below the grid and copy the following between "script" tags
var trRows = document.getElementsByTagName('tr');
var countRows = trRows.length;
for (var i = 0; i < countRows; ++i) 
{
    if (trRows[i].getAttribute('isexp') != null && trRows[i].getAttribute('isexp').toLowerCase() == 'true')
    {
        if(trRows[i].firstChild.firstChild != null && trRows[i].firstChild.firstChild.tagName.toLowerCase() == 'a')
        {  
            {
                trRows[i].firstChild.firstChild.fireEvent('onclick');
            }
        }
    }
}

1 comment:

  1. Below is jQuery workaround from this link https://adrianhenke.wordpress.com/2010/04/16/sharepoint-spgridview-control-is-expanded-by-default/ :-

    function spGridViewFixExpandedGroups(gridviewId) {
    var gridview = $(“[id$='” + gridviewId + “‘]”);
    var links = gridview.find(“tr[isexp=’true’]”).find(“a[title*=’Expand/Collapse’]”);
    links.click();
    }

    ReplyDelete