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

如何添加按钮以在最后一个标签附近添加新标签?,第1张

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

您可以

<p:tabView>
用来显示基于某些bean集合的动态选项卡集。您可以将“添加”选项卡显示为选项卡集的最后一个选项卡。您甚至可以根据需要更改其样式。您可以使用
<p:ajaxevent="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@ViewScopedpublic 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
在此示例中,该类只是具有属性
title
和的普通javabean
content
。该
oncomplete
<p:ajax>
是必要的,因为标签内容将添加新标签后否则消失(这看起来很像一个错误PF,毕竟,我用3.3的方式)。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5490730.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存