4
$\begingroup$

Would someone have an idea on how to implement a table with rows that you can expand or collapse in a grid. An example could be found in a pivottable in Excel, for example here http://www.contextures.com/excelpivottableexpandcollapse.html.

I would use it for example to display the total of groups of entities and be able to see the components of the sum.

Such hierarchical table could be useful in many situations.

The question is just on the display aspect, not about pivottable.

$\endgroup$
0

2 Answers 2

7
$\begingroup$
data = RandomInteger[10, {5, 10, 5}];

OpenerView

Column[Map[OpenerView[{Grid[{Total@#}, Dividers -> All, ItemSize -> 3, Background -> Pink],
      Grid[#, Dividers -> All, ItemSize -> 3, Background -> LightBlue]}] &, data]]

enter image description here

FlipView

Column[Map[FlipView[{Grid[{Total@#}, Dividers -> All, ItemSize -> 3, Background -> Pink],
     Grid[Prepend[#, Total@#], Dividers -> All, ItemSize -> 3, 
          Background -> {LightBlue, 1 -> Pink}]}, 1] &, data]]

enter image description here

See also:

tutorial / Views >> OpenerView

tutorial / Views >> FlipView

$\endgroup$
2
  • $\begingroup$ Very nice, thank you. $\endgroup$
    – faysou
    Nov 9, 2014 at 8:51
  • $\begingroup$ @faysou, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Nov 9, 2014 at 9:18
2
$\begingroup$

Here's a generalisation of kguler's answer for OpenerView. It allows to handle tree-like structures.

Using the functions defined in my answer to Nested OpenerView controls not aligned you can emulate OpenerView controls that nest well (i.e are aligned, which is not possible using OpenerView because of the recursive way it's built, there's always an extra space in sub layers). You need to copy the code from the previous link before execting this code. This code deals more with the recursion on a tree rather than with the display which is now easily handled with openerView and OpenerTree.

ClearAll@grid;
grid[x_,color_:None]:=Grid[{x},Dividers->All,ItemSize->4,Background->color];

leafExpand[object_]:=
    Module[{info,color},
        info=getLeafInfo@object;
        color=getLeafColor@object;

        {
            info
            ,
            grid[info,color]
        }
    ];

recursiveExpand[object_]:=
    Module[{recursiveExpansion,results,displays,result,display,children,color},

        children=getChildren@object;
        recursiveExpansion=TreeExpand/@children;

        color=getGroupColor@object;

        results=recursiveExpansion[[All,1]];
        displays=recursiveExpansion[[All,2]];

        result=groupAggregationFunction@results;

        display=openerView[grid[result,color],displays];

        {result,display}
    ];

ExpandTree[x_,header_]:=Map[TreeExpand,x][[All,2]]//OpenerTree[#,header]&;

getChildren=#[[2]]&;
groupAggregationFunction=Total;
getGroupColor=First;
getLeafInfo={#[[2]],#[[2]]}&;
getLeafColor=First;

TreeExpand[o:oo[color_,info_]]^:=leafExpand[o]
TreeExpand[o:gg[color_,info_]]^:=recursiveExpand[o];

x={gg[Pink,{gg[Pink,{oo[LightBlue,1],oo[LightBlue,2],oo[LightBlue,3]}],oo[LightBlue,1],oo[LightBlue,2],oo[LightBlue,3],gg[Green,{oo[LightBlue,1],gg[Black,{oo[LightBlue,1],oo[LightBlue,2],oo[LightBlue,3]}],oo[LightBlue,2],oo[LightBlue,3]}]}]};

ExpandTree[x,grid@{"a","b"}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.