我正在尝试使用其中包含inputTextArea组件的Primefaces按需加载选项卡式面板组件。我按照本教程Primefaces Tutorial进行操作。 我的.xhtml表单如下所示:
<h:form>
<h:outputText
value="Write a comment" />
<p:tabView dynamic="true" cache="true">
<p:tab title="Possitive">
<h:panelGrid columns="2" cellpadding="10">
<p:inputTextarea rows="5" cols="130" />
</h:panelGrid>
</p:tab>
<p:tab title="Negative">
<h:panelGrid columns="2" cellpadding="10">
<p:inputTextarea rows="5" cols="130" />
</h:panelGrid>
</p:tab>
</p:tabView>
<p:commandButton value="Publish"></p:commandButton>
</h:form>
但是这些inputTextBox总是被加载,但是我没有点击它们。它们只能按需加载吗?怎么做?
提前感谢您的帮助!
答案 0 :(得分:1)
Primefaces为所有标签构建JSF树。两个inputTextarea都在服务器端创建。它可以通过ui解决:include。将p:tab的内容放入额外文件中,并使用ui:include来获取内容:
<p:tabView>
<p:tab title="Possitive">
<ui:include src="#{tabViewBean.getTabSource('tabPositive')}"/>
</p:tab>
<p:tab title="Negative">
<ui:include src="#{tabViewBean.getTabSource('tabNegative')}"/>
</p:tab>
</p:tabView>
TabViewBean包含活动选项卡名称,也用于解析选项卡源。如果选项卡非活动,则方法tabViewBean.getTabSource()将返回另一个包含 EMPTY 内容的文件:
public String getTabSource(String tabName)
{
return tabName.equals(activeTabName) ?
getTabSourceForTabName(tabName) :
"EmptyTab.xhtml";
}
public String getTabSourceForTabName(String tabName)
{
if ("tabPositive".equals(tabName)) return "TabPositive.xhtml";
if ("tabNegative".equals(tabName)) return "TabNegative.xhtml";
}
你应该在某个地方有EmptyTab.xhtml。在p:tabView中设置监听器以监听选项卡更改事件:
<p:tabView listener="#{tabViewBean.tabChangeListener}">
tabChange侦听器设置活动选项卡的名称:
public void tabChangeListener(UIComponentBase tabView)
{
activeTabName = getTabNameByIndex(tabView.getActiveIndex());
}
public String getTabNameByIndex(int index)
{
if (i==0) return "tabPositive";
if (i==1) return "tabNegative";
}