How can we globally expand / collapse for a JQGrid when we have rows grouped by some field?

How can we globally expand / collapse for a JQGrid when we have rows grouped by some field?

When expanding, it should expand all groups and collapse all groups should be collapsed.

+5
source share
3 answers

You can set the default value for the groupCollapseparameter property groupingViewfor jqGrid just as you set any other default parameter:

$.extend($.jgrid.defaults, {
    groupingView: {
        groupCollapse: true
    }
});

. , , /, - /.

var $grid = $("#list"), inOnClickGroup = false;

$grid.jqGrid({
    // ... other options
    grouping: true,
    onClickGroup: function (hid) {
        var idPrefix = this.id + "ghead_", id, i, l,
            groups = this.p.groupingView.sortnames[0];

        if (!inOnClickGroup && hid.length > idPrefix.length &&
                hid.substr(0, idPrefix.length) === idPrefix) {
            id = Number(hid.substr(idPrefix.length));
            if (typeof (groups[id]) !== "undefined") {
                inOnClickGroup = true; // set to skip recursion
                for (i = 0, l = groups.length; i < l; i++) {
                    if (i !== id) {
                        $(this).jqGrid('groupingToggle', this.id + 'ghead_' + i);
                    }
                }
                inOnClickGroup = false;
            }
        }
    }
});

.

+3
$('#grid-expand-collapse').change(function () {

    var idPrefix = "MyGridghead_", index, length, tarspan;
    var groups = $(options.gridElement)[0].p.groupingView.sortnames[0];

    if ($(this).is(':checked')) {

        for (index = 0, length = groups.length; index < length; index++) {

            tarspan = $("#MyGridghead_" + index + " span." + "tree-wrap-" + $(options.gridElement)[0].p.direction);
                if (!tarspan.hasClass($(options.gridElement)[0].p.groupingView.minusicon)) {
                    $(options.gridElement).jqGrid('groupingToggle', 'MyGridghead_' + index);
                }
        }
    }
    else {
        for (index = 0, length = groups.length; index < length; index++) {

            tarspan = $("#MyGridghead_" + index + " span." + "tree-wrap-" + $(options.gridElement)[0].p.direction);
            if (tarspan.hasClass($(options.gridElement)[0].p.groupingView.minusicon)) {
                $(options.gridElement).jqGrid('groupingToggle', 'MyGridghead_' + index);
            }
        }
    }

});
+1
$('#jqxGrid').jqxGrid({
    groupsexpandedbydefault: true
});

worked like a charm for me ( source ).

-3
source

All Articles