How to collapse a categorized view in an XPage

If you've got a categorized view and you want to display it in an XPage, that bit is simple, but what if you want to add buttons which allow the user to expand and collapse the view? This is one of those things which is unnecessarily complicated in XPages, there's no checkbox or simple command and there is a gotcha in the view design as well.

So the first step is to write the onClick code for the collapse button:

var viewPanel = getComponent("viewPanel1");
var model:com.ibm.xsp.model.domino.DominoViewDataModel = viewPanel.getDataModel();
var container:com.ibm.xsp.model.domino.DominoViewDataContainer = model.getDominoViewDataContainer();
container.collapseAll();

The same code can be used for the expand button, just replace the last line with "container.expandAll()".

So that's painful to write from scratch, but something which can be copied and pasted in future. But now for the problem.

If you click the collapse all button and all of the data in the view disappears then you'll need to go into your view design, go to the third tab of the view properties and make sure that the "Don't show empty categories" is *not* checked. This seems to be a bug which was introduced some time in the 8.5.1 / 8.5.2 timeframe.