如何在最后一个标签附近添加添加新标签的按钮?

San*_*nsa 5 java jsf primefaces jsf-2

有没有人知道如何添加一个按钮来添加位于最后创建的选项卡附近的新选项卡?如果不清楚我的情况,只需查看浏览器中的标签和添加新标签的按钮;-)这正是我想要管理的内容.

我正在使用MyFaces + PrimeFaces.

Bal*_*usC 6

您可以使用<p:tabView>基于某些bean集合来显示动态tabset.您可以将"添加"选项卡显示为选项卡集的最后一个选项卡.如有必要,您甚至可以采用不同的方式.您可以使用<p:ajax event="tabChange">挂钩选项卡更改侦听器,您可以在其中检查是否已打开"添加"选项卡,然后添加新选项卡.

例如

<h:form id="form">
    <p:tabView id="tabs" value="#{bean.tabs}" var="tab" widgetVar="w_tabs">
        <p:ajax event="tabChange" listener="#{bean.changeTab}" oncomplete="if (args.newTabIndex) w_tabs.select(args.newTabIndex)" />
        <p:tab title="#{tab.title}">
            <p>#{tab.content}</p>
        </p:tab>
    </p:tabView>
</h:form>
Run Code Online (Sandbox Code Playgroud)

同

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private List<Tab> tabs;

    @PostConstruct
    public void init() {
        // Just a stub. Do your thing to populate tabs.
        // Make sure that the last tab is the "Add" tab.
        tabs = new ArrayList<Tab>();
        tabs.add(new Tab("tab1", "content1"));
        tabs.add(new Tab("tab2", "content2"));
        tabs.add(new Tab("Add...", null));
    }

    public void changeTab(TabChangeEvent event) {
        int currentTabIndex = ((TabView) event.getComponent()).getActiveIndex();
        int lastTabIndex = tabs.size() - 1; // The "Add" tab.

        if (currentTabIndex == lastTabIndex) {
            tabs.add(lastTabIndex, new Tab("tab" + tabs.size(), "content" + tabs.size())); // Just a stub. Do your thing to add a new tab.
            RequestContext requestContext = RequestContext.getCurrentInstance();
            requestContext.update("form:tabs"); // Update the p:tabView to show new tab.
            requestContext.addCallbackParam("newTabIndex", lastTabIndex); // Triggers the oncomplete.
        }
    }

    public List<Tab> getTabs() {
        return tabs;
    }

}
Run Code Online (Sandbox Code Playgroud)

的Tab类是在本例中只是一个普通的JavaBean具有属性title和content.该oncomplete中<p:ajax>是必要的,因为标签内容将添加新标签后否则消失(这看起来很像一个错误PF,毕竟,我用3.3的方式).