5

最後に作成したタブの近くに新しいタブを追加するためのボタンを追加する方法を知っている人はいますか?私が何をしているのかわからない場合は、ブラウザのタブと新しいタブを追加するためのボタンを見てください;-)まさにそれが私が管理したいものです。

MyFaces+PrimeFacesを使用しています。

4

1 に答える 1

6

<p:tabView>Beanのコレクションに基づいて動的なタブセットを表示するために使用できます。「追加」タブをタブセットの最後のタブとして表示できます。必要に応じて、スタイルを変えることもできます。タブ変更リスナーをフックするために使用できます<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>

@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;
    }

}

この例では、クラスはプロパティと。Tabを持つ単なるJavaBeanです。新しいタブを追加するとタブのコンテンツが消えてしまうため、inが必要です(結局、PFのバグのように見えます。ちなみに私は3.3を使用していました)。titlecontentoncomplete<p:ajax>

于 2012-08-15T00:31:47.817 に答える