How to switch p:tab in the same form using a command button
How do I switch to a开发者_开发问答 tab (<p:tab>
) using a command button?
There is also a client side api method called selectTab(index);
<p:commandButton type="button" onclick="widgetvar.selectTab(2)" value="Show" />
p:tabView
has an attribute activeIndex
that is the "Index of the active tab" (Primefaces Documentation).
You could set this attribute from the action method of your p:commandButton
:
<p:commandButton value="Switch tab" action=#{myBean.switchTab} />
Define an action method switchTab() in your backing bean and let it set a member activeTab
.
Then use this member to set your active tab
<p:tabView activeIndex=#{myBean.activeTab}>
If your server supports EL 2.2 you can set the index of the active tab with the action method call:
<p:commandButton value="Switch tab" action=#{myBean.switchTab(2)} />
Then you can use the argument of your action method call to set the active index directly.
I use Primefaces 5.1 and what I did was bind my tabView in the ManagedBean and set the activeIndex there
In your JSF
<h:form prependId="false" id="form">
<p:tabView id="tabPanel" widgetVar="tabPanel" binding="#{managedBean.tabView}" dynamic="true">
<p:tab title="tab" >
<p:commandButton action="#{managedBean.getBla}"
icon="ui-icon-search" update=":form:tabPanel" immediate="true" >
...
In your ManagedBean
private TabView tabView;
public TabView getTabView() {
return tabView;
}
public void setTabView(TabView tabView) {
this.tabView = tabView;
}
And then in the method you call in your commandButton action you just do a
tabView.setActiveIndex(1);
Hope it works :)
To switch p:tab in the same form using a command button At the Client side only you can use widgetVar to select/view tab as follow:
Note: tabIndex will start from 0 for the first tab.
<p:commandButton type="button" onclick="PF('tabWidgetVar').select(1)" value="Next" />
At the server side you can bind a integer variable with the activeIndex property of the p:tab and then excute method to set index.
精彩评论