1

Is there a way we can implement expand all/collapse all functionality for grouping feature in jqgrid? As I understand setting groupingCollapse: true, collapses the data but I would like to make it dynamic, may be on the click of +/- icons placed within the grid. Thanks in advance for any suggestions/help...

varaprakash
  • 487
  • 4
  • 12
  • 30

3 Answers3

1

I think you will find the answer on your question in the old answer. The main idea of the answer is to use sortnames[0] property of the parameter groupingView of jqGrid. It is an array which elements can be used to constructs the ids of grouping headers and one can use groupingToggle method to collapse or expand the group header. In the way you can expand or collapse of all groups.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @Oleg Is there a way, I could collapse all groups in a grid when initially `groupCollapse` is `false`? I do not want the grid to collapse when it gets refreshed. – Suhail Gupta Nov 04 '15 at 08:03
  • @SuhailGupta: Sorry, but you wrote what you don't want, but I still don't understand your question. One can use `groupingToggle` method to expand/collapse any group from the grid. To get the names of the group(s) (the parameter of `groupingToggle`) one need use `id` from the gruop header row or one can construct it based in the information from `groupingView`, but the structure of `groupingView` depends on the version of jqGrid. See [the answer](http://stackoverflow.com/a/28705221/315935) for additional code fragment. What is youe question? What you need to implement? – Oleg Nov 04 '15 at 08:15
  • @Oleg I want to keep the groups in the expanded state, when the grid is refreshed. – Suhail Gupta Nov 04 '15 at 08:19
  • @SuhailGupta: Then just remove `groupCollapse: false`. Probably you should open new question where you describe your scenario and the problem of the implementation. – Oleg Nov 04 '15 at 08:22
  • @Oleg I don't want to. Is there any way we could keep the groups in the same state as before refresh? – Suhail Gupta Nov 04 '15 at 08:23
  • @SuhailGupta: you speak about some scenario without to describe what you do. Which `datatype` you use? Which version of jqGrid and which fork you use? Do you use `loadonce: true` or not? Do you need to reload **locally** or from the server? Reloading reset page number tupically to 1. Do you make some changes **before reloading** (changeing of prouping parameters and so on)? Do you need to hold the page number? You can **save** the state of opened/collapes groups, and set the same state after reloading, but the groups will be identified by group header id, which could be in some cases *another* – Oleg Nov 04 '15 at 09:44
  • @Oleg I use version 4.6. I need to refresh grid at few seconds with data from the database, so I do not use `loadonce:true`. How do I save the state of grid with opened/collapsed groups? – Suhail Gupta Nov 05 '15 at 04:14
  • @SuhailGupta: Look at [the answer](http://stackoverflow.com/a/9202378/315935), which demonstrates how to persist expand/collapse state in TreeGrid. One can use the same idea, but the implementation will be another of cause. What you ask is not a mini-question for answering in comment. You should provide the demo which demonstrates the starting point. I asked you many questions in my previous comment, but you answered only subset . You should described **detailed** the use case and the problem. – Oleg Nov 05 '15 at 05:58
1

This is specific to a particular version of jqgrid, and it expands all layers, but it's so short and sweet, I just can't resist posting it.

function expandAll() {
    $("#myGrid .ui-icon-circlesmall-plus").trigger("click");
}
0

With Oleg's answer/suggestion and Jqgrid4.4.1, here is what I have done. This works when you want to expand/collapse all rows on click of some button...

function expandCollapseGroups(expandAll) {
    var $grid = $("#grid");
    var idPrefix =$grid[0].id + "ghead_0_", trspans;
    var groups =$grid[0].p.groupingView.groups;
    if ($grid[0].p.grouping) {
        for (var index = 0; index < groups.length; index++) {
            if (expandAll) {
                trspans = $("#" + idPrefix + index + " span.tree-wrap-" +$grid[0].p.direction + "." +$grid[0].p.groupingView.plusicon);
            } else {
                trspans = $("#" + idPrefix + index + " span.tree-wrap-" +$grid[0].p.direction + "." +$grid[0].p.groupingView.minusicon);
            }
                if (trspans.length > 0) {
                $grid.jqGrid('groupingToggle', idPrefix + index);
        }   
        }
    } 
}
varaprakash
  • 487
  • 4
  • 12
  • 30