Pranjali
Pranjali

Reputation: 535

How to remove MenuItem from DefaultMenuModel of Primefaces Breadcrumb

Hi I am building DefaultMenuModel programmatically which is being used by the component as follows

`<p:breadCrumb id="pbreadcrumb" model="#{portalNavigator.model}"/>`

and this is the code from PortalNavigator Bean which adds MenuItem to object model which is of type DefaultMenuModel.

DefaultMenuModel model=new DefaultMenuModel();    
MenuItem item=new MenuItem();  
item.setId("home");  
item.setUrl("/getPortal");  
item.setValue("Home");  
model.addMenuItem(item);

My question is How can I remove a MenuItem from DefaultMenuModel,what is the way to do it?

Upvotes: 2

Views: 1787

Answers (1)

BalusC
BalusC

Reputation: 1108632

You can get them all by DefaultMenuModel#getContents() which returns a List<UIComponent> whose items you can cast back to MenuItem. Loop over them in an Iterator. Once you found the item you want to remove, use Iterator#remove() method.

Iterator<UIComponent> iterator = model.getContents().iterator();

while (iterator.hasNext()) {
    MenuItem item = (MenuItem) iterator.next();

    if (needsRemoval(item)) {
        iterator.remove();
    }
}

Or, if you already know the index beforehand, you can also just do:

model.getContents().remove(index);

Upvotes: 3

Related Questions