【发布时间】:2026-01-05 19:10:01
【问题描述】:
我确实有一个页面,其中有一个包含输入元素的 rich:collapsiblePanel 列表。这些 collapsiblePanel 本身将它们的展开/折叠状态存储在一个支持 bean 中。
现在我有了一个用例,只需单击鼠标即可一次打开/关闭所有这些 collapsiblePanel。所以我试图用列表中的两个命令按钮来实现这一点。它们使用附加的 actionListener 来遍历 collapsiblePanels 的所有支持 bean,并将扩展标志设置为 true/false。
这似乎可行,除非您自己打开或关闭其中一个 collapsiblePanel。一旦发生这种情况,单击按钮将不再执行任何操作。
<h:form prependId="false">
<a4j:commandButton value="Open All" actionListener="#{viewBean.doOpenAll}" render="c" />
<a4j:commandButton value="Close All" actionListener="#{viewBean.doCloseAll}" render="c" style="margin-left: 10px;" />
<a4j:outputPanel id="c">
<a4j:repeat id="repeat" value="#{viewBean.items}" var="item">
<rich:collapsiblePanel id="panel" expanded="#{item.expanded}">
<h:outputLabel id="text_lbl" value="text" />
<h:inputText id="text" value="#{item.text}" />
</rich:collapsiblePanel>
</a4j:repeat>
</a4j:outputPanel>
</h:form>
我已经发布了a project on github,以便您可以尝试使用代码。
为了完整起见,这里有两个支持 bean
@ViewScoped
@ManagedBean
public class ViewBean implements Serializable {
static final Logger LOG = LoggerFactory.getLogger(ViewBean.class);
private static final long serialVersionUID = -6239437588285327644L;
private List<ListItem> items;
public ViewBean() {
items = new ArrayList<ListItem>(10);
for (int i = 0; i < 10; i++) {
items.add(new ListItem("item " + i));
}
}
public void doOpenAll() {
LOG.debug("open all");
for (ListItem item : items) {
item.setExpanded(true);
}
}
public void doCloseAll() {
LOG.debug("close all");
for (ListItem item : items) {
item.setExpanded(false);
}
}
public List<ListItem> getItems() {
return items;
}
}
public class ListItem {
private boolean expanded;
private String text;
public ListItem(String text) {
super();
this.text = text;
}
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
【问题讨论】: