MenuButton groups multiple items in a popup.
<div class="card">
<h:form>
<p:growl id="messages" showDetail="true"/>
<h5 class="mt-0">Basic</h5>
<p:menuButton value="Options">
<p:menuitem value="Save (Non-Ajax)" action="#{menuView.save}" ajax="false" update="messages" icon="pi pi-save"/>
<p:menuitem value="Update" action="#{menuView.update}" update="messages" icon="pi pi-refresh"/>
<p:menuitem value="Delete" action="#{menuView.delete}" update="messages" icon="pi pi-times"/>
<p:divider/>
<p:menuitem value="Website" url="http://www.primefaces.org" icon="pi pi-external-link"/>
</p:menuButton>
<h5>Button style</h5>
<p:menuButton value="Options" buttonStyleClass="ui-button-outlined" buttonStyle="border-radius: 0 1rem">
<p:menuitem value="Save (Non-Ajax)" action="#{menuView.save}" ajax="false" update="messages" icon="pi pi-save"/>
<p:menuitem value="Update" action="#{menuView.update}" update="messages" icon="pi pi-refresh"/>
<p:menuitem value="Delete" action="#{menuView.delete}" update="messages" icon="pi pi-times"/>
<p:divider/>
<p:menuitem value="Website" url="http://www.primefaces.org" icon="pi pi-external-link"/>
</p:menuButton>
<h5>Ajax Disabling and Status Icon</h5>
<p:menuButton value="Options">
<p:menuitem value="Save" action="#{menuView.sleepAndSave}" update="messages" icon="pi pi-save"/>
<p:menuitem value="Update" action="#{menuView.sleepAndUpdate}" update="messages" icon="pi pi-refresh"/>
<p:menuitem value="Delete" action="#{menuView.sleepAndDelete}" update="messages" icon="pi pi-times"/>
</p:menuButton>
</h:form>
</div>
@Named
@ViewScoped
public class MenuView implements Serializable {
private static final long serialVersionUID = 1L;
private MenuModel model;
@PostConstruct
public void init() {
model = new DefaultMenuModel();
//First submenu
DefaultSubMenu firstSubmenu = DefaultSubMenu.builder()
.label("Options")
.expanded(true)
.build();
DefaultMenuItem item = DefaultMenuItem.builder()
.value("Save (Non-Ajax)")
.icon("pi pi-save")
.ajax(false)
.command("#{menuView.save}")
.update("messages")
.build();
firstSubmenu.getElements().add(item);
item = DefaultMenuItem.builder()
.value("Update")
.icon("pi pi-refresh")
.command("#{menuView.update}")
.update("messages")
.build();
firstSubmenu.getElements().add(item);
item = DefaultMenuItem.builder()
.value("Delete")
.icon("pi pi-times")
.command("#{menuView.delete}")
.update("messages")
.build();
firstSubmenu.getElements().add(item);
model.getElements().add(firstSubmenu);
//Second submenu
DefaultSubMenu secondSubmenu = DefaultSubMenu.builder()
.label("Navigations")
.expanded(false)
.build();
item = DefaultMenuItem.builder()
.value("Website")
.url("http://www.primefaces.org")
.icon("pi pi-external-link")
.build();
secondSubmenu.getElements().add(item);
item = DefaultMenuItem.builder()
.value("Internal")
.icon("pi pi-upload")
.command("#{menuView.redirect}")
.build();
secondSubmenu.getElements().add(item);
model.getElements().add(secondSubmenu);
}
public MenuModel getModel() {
return model;
}
public void redirect() throws IOException {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath());
}
public void save() {
addMessage("Save", "Data saved");
}
public void update() {
addMessage("Update", "Data updated");
}
public void delete() {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Delete", "Data deleted");
FacesContext.getCurrentInstance().addMessage(null, message);
}
public void sleepAndSave() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
save();
}
public void sleepAndUpdate() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
update();
}
public void sleepAndDelete() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
delete();
}
public void addMessage(String summary, String detail) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
FacesContext.getCurrentInstance().addMessage(null, message);
}
}