I have a composite component (collapsiblePanel). The component uses the \"collapsible\" bean to provide the toggle function. When I use the same component multiple times on
You can use the componentType
attribute of the <cc:interface>
to define a "backing component".
E.g.
<cc:interface componentType="collapsiblePanel">
...
</cc:interface>
<cc:implementation>
...
<h:commandButton action="#{cc.toggle}" ... />
...
<h:panelGroup rendered="#{!cc.collapsed}" ...>
...
</cc:implementation>
with just a com.example.components.CollapsiblePanel
@FacesComponent(value="collapsiblePanel") // To be specified in componentType attribute.
public class CollapsiblePanel extends UINamingContainer { // Important! Must extend UINamingContainer.
private boolean collapsed;
public void toggle() {
collapsed = !collapsed;
}
public boolean isCollapsed() {
collapsed;
}
}
However, when you want to have multiple of those components, then you should declare physically separate instances of them in the view. If this needs to happen dynamically, then you need to use <c:forEach>
to generate physically separate instances of them instead of <ui:repeat>
with a single component. Otherwise you have to map all collapsed
states by the client ID inside a Map<String, Boolean>
. See for an example and more background information also Getting same instance of `componentType` in composite component on every use