primefaces tabView activeIndex 问题

发布于 2024-10-21 05:37:45 字数 384 浏览 6 评论 0原文

我有 Primefaces TabView 和两个选项卡,如下所示:

<p:tabView dynamic="true" cache="false"
           onTabShow="scrollBottom(#{stanzaBean.activeIndex})"
           tabChangeListener="#{messaggioBean.onTabChange}"
           activeIndex="#{stanzaBean.activeIndex}" >

它工作正常,除了当我更改选项卡时,activeIndex 不会在服务器上更新,并且它始终返回默认值。 我正在使用 primefaces 2.2.1。

谢谢。

I have Primefaces TabView with two Tab like:

<p:tabView dynamic="true" cache="false"
           onTabShow="scrollBottom(#{stanzaBean.activeIndex})"
           tabChangeListener="#{messaggioBean.onTabChange}"
           activeIndex="#{stanzaBean.activeIndex}" >

it works fine, except that when I change the Tab the activeIndex isn't updated on the Server and it returns always the default value.
I'm using primefaces 2.2.1.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清眉祭 2024-10-28 05:37:45

按照 PrimeFaces ShowCase 示例,如果您为每个选项卡指定一个 id:

<p:tabView tabChangeListener="#{indexBean.onTabChange}" >
    <p:tab title="tab 0" id="tab0"></p:tab>
    <p:tab title="tab 1" id="tab1" ></p:tab>
    <p:tab title="tab 2" id="tab2"></p:tab>               
</p:tabView>

您可以在 tabChangeListener 中获取该选项卡 id。

public void onTabChange(TabChangeEvent event) {       
    System.out.println("tab id = " + event.getTab().getId());
}

然后您就会知道选择了哪个选项卡。


编辑:

有一个开放的 PrimeFaces 问题 1640 TabView:TabChangeListener 中的 activeIndex 错误,始终为 0 关于您遇到的问题。


编辑 2:

在 PrimeFaces 5.0 及更高版本中,tabChangeListenertabView 元素上不再可用,但应通过带有 tabChange 事件的显式 ajax 标记来使用。

 <p:tabView id="analysisSections" value="#{analysisBean.analysis.sections}" var="section" activeIndex="#{analysisBean.activeIndex}">
      <p:ajax event="tabChange" listener="#{analysisBean.onTabChange}"/>

您还可以直接获取选项卡的索引:

public void onTabChange(TabChangeEvent event) {
    activeIndex = ((TabView) event.getSource()).getIndex();
}

通过所有这些更改,activeIndex 可以正常工作。

Going by the PrimeFaces ShowCase example, if you give each tab an id:

<p:tabView tabChangeListener="#{indexBean.onTabChange}" >
    <p:tab title="tab 0" id="tab0"></p:tab>
    <p:tab title="tab 1" id="tab1" ></p:tab>
    <p:tab title="tab 2" id="tab2"></p:tab>               
</p:tabView>

you can get that tab id in the tabChangeListener.

public void onTabChange(TabChangeEvent event) {       
    System.out.println("tab id = " + event.getTab().getId());
}

Then you'll know which tab was selected.


Edit:

There is an open PrimeFaces issue 1640 TabView: Wrong activeIndex in TabChangeListener, always 0 on the problem you are having.


Edit 2:

With PrimeFaces 5.0 and up the tabChangeListener is no longer available on the tabView element but should be used via an explicit ajax tag with a tabChange event.

 <p:tabView id="analysisSections" value="#{analysisBean.analysis.sections}" var="section" activeIndex="#{analysisBean.activeIndex}">
      <p:ajax event="tabChange" listener="#{analysisBean.onTabChange}"/>

Also you can directly get index of tab:

public void onTabChange(TabChangeEvent event) {
    activeIndex = ((TabView) event.getSource()).getIndex();
}

with all these changes, activeIndex works properly.

一腔孤↑勇 2024-10-28 05:37:45

这对我有用:

public void onTabChange(TabChangeEvent event) {
        Tab activeTab = event.getTab();
        tabPanelIndex = ((TabView)event.getSource()).getChildren().indexOf(activeTab);
    }

this worked for me:

public void onTabChange(TabChangeEvent event) {
        Tab activeTab = event.getTab();
        tabPanelIndex = ((TabView)event.getSource()).getChildren().indexOf(activeTab);
    }
守望孤独 2024-10-28 05:37:45

虽然问题与 PrimeFaces 2.2.1 有关,但我想提一下,在现代 PrimeFaces 版本(使用版本 6.2 进行测试)中,当属性 dynamic 设置为 时,不需要触发单独的事件>truecache 设置为 false。通过使用此属性组合,当选择另一个选项卡时,活动索引会在服务器上自动更新。

Facelet:

<p:tabView activeIndex="#{stanzaBean.activeIndex}"
           cache="false"
           dynamic="true">

Bean:

@Named
@ViewScoped
public class StanzaBean implements Serializable {

    private int activeIndex;

    public int getActiveIndex() {
        return activeIndex;
    }

    /**
     * Automatically called whenever a tab changes and dynamic="true"
     * and cache="false".
     */
    public void setActiveIndex(int activeIndex) {
        this.activeIndex = activeIndex;

        // do other stuff when tab changes
    }

}

Although the question was related to PrimeFaces 2.2.1, I like to mention that in modern PrimeFaces versions (tested with version 6.2) there is no need to trigger a separate event when attribute dynamic is set to true and cache is set to false. By using this attribute combination the active index is automatically updated on the server when another tab is selected.

Facelet:

<p:tabView activeIndex="#{stanzaBean.activeIndex}"
           cache="false"
           dynamic="true">

Bean:

@Named
@ViewScoped
public class StanzaBean implements Serializable {

    private int activeIndex;

    public int getActiveIndex() {
        return activeIndex;
    }

    /**
     * Automatically called whenever a tab changes and dynamic="true"
     * and cache="false".
     */
    public void setActiveIndex(int activeIndex) {
        this.activeIndex = activeIndex;

        // do other stuff when tab changes
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文