0

I am programmatically add a new tab whenever a menu item is clicked. I have googled for all the questions on dynamic Tab creation and found a lot of unanswered queries. So my question is, is it possible to create tabs dynamically? Below is the code I am working on for the past week and not a tab in sight..

I am using Primefaces 5.0, JSF 2.2 Mojarra

Dynamic menu -

DefaultMenuItem  windowItem = new DefaultMenuItem(node);
windowItem.setCommand("#{windowContainer.add}");
windowItem.setImmediate(true);

WindowContainer Backing Bean

@ManagedBean
@ViewScoped
public class WindowContainer implements Serializable {

private List<Window> windows;

@PostConstruct
public void init() {
    windows = new ArrayList<Window>();
    windows.add(new Window("window"+windows.size(),"first tab"));
}

public void add() {
   windows.add(new Window("window" + windows.size(), "some content"));
   RequestContext req = RequestContext.getCurrentInstance();
   req.update("WindowContainer");
}

public void remove(Window window) {
    windows.remove(window);
}

public List<Window> getWindows() {
    return windows;
}

public void setWindows(ArrayList<Window> windows) {
   this.windows = windows;
}

Windows is simple tab with only outputtext .

The View

   <p:tabView id= "WindowContainer" value="#{windowContainer.windows}" var="window">  
    <p:tab title="#{window.title}">
       <p>#{tab.content}</p>                        
           <p:commandButton value="Close" action="#{windowContainer.remove(window)}"  />
       </p:tab>
  </p:tabView> 
Cœur
  • 37,241
  • 25
  • 195
  • 267
jjj-rm
  • 1
  • Had a look at [this](http://stackoverflow.com/questions/22293708/how-to-add-tabs-in-tabview-in-primefaces-dynamically-on-click-of-a-command-but)? – ForguesR Oct 03 '14 at 15:13

1 Answers1

0
<p:menu>
    <p:submenu label="Ajax">
        <p:menuitem value="Add Tab" actionListener="#{menuView.addTab}" update="WindowContainer"  />
</p:menu>

Try like this update="WindowContainer" .Inside quotes give tabview id.

Binief Ta
  • 1
  • 1
  • Can you explain **why** this works? – rayryeng May 14 '15 at 07:03
  • I'm facing a similar problem. I want to add a new tab from a menu link. The tab content need to be loaded from a xhtml file. Please let me know if it worked in your case and how you do it. Thanks you! – Néstor Almeida Nov 09 '17 at 09:09