I'm trying to display a tree in JSF without hard-coding the depth of the tree I want to display. Maybe for one configuration I only want to show the leaves, maybe for another I want to show the leaves grouped by the nodes one level above them, etc. So for one configuration, it could be:
While for another configuration, the same underlying data structure could produce:
- Question 1
- Question 2
- Question 3
- Question 4
With a hypothetical third configuration grouping the categories by super-categories, etc.
I tried making a composite component that called itself recursively if the node had more children. I did this via <ui:fragment rendered="#{node.hasChildren}">
and -- surprise! -- this resulted in an infinite loop and a stack overflow.
The project already has PrimeFaces installed, so I'm looking at PrimeFaces Tree
and TreeNode
. But that doesn't quite feel right; I don't want the tree nodes to be expandable in the user interface. I want everything to be fully expanded. I haven't gone too deeply down this rabbit hole so I suspect there may be a way to achieve this with Tree
, but it feels like I'm going against the grain of what it's built to do.
What's a good approach this problem?
Edit: I'm trying to use <tree> and <treeNode>, but it's not working. My code looks like this:
<p:tree value="#{cc.attrs.classification}" var="child">
<p:treeNode>
<ui:fragment rendered="#{child.childCount > 0}">
[do node stuff]
</ui:fragment>
<ui:fragment rendered="#{child.childCount == 0}">
[do leaf stuff]
</ui:fragment>
</p:treeNode>
</p:tree>
... where classification
implements the TreeNode
interface. This results in errors to the console complaining that child
is of type String and therefore cannot do any of the interesting things I'm asking of it. What am I doing wrong?