Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces 4, dynamic menu setCommand method

I'm trying primefaces 4 but there are no documentation around for the new MenuModel. Here, Optimus Prime wrote about the new menu system with a little example. http://blog.primefaces.org/?p=2594

At this point, he wrote about a setCommand method:

setCommand

This point to a save method (found in the pf4 showcase: http://www.primefaces.org/showcase/ui/menu/menu.xhtml):

save method

After this introduction, here's the question/problem. I'm creating a dynamic menu from a bean but I don't understand how know the menu clicked by the user and do the right operation.

public void init() {
        if (spBean == null) {
            System.out.println("spBean is NULL!");
            return;
        }
        for (ServiceProvider sp: spBean.getListaSP()) {
            DefaultMenuItem item = new DefaultMenuItem(sp.getNome());
            //item.setUrl("#");
            item.setIcon("images/sps/" + sp.getImageId() + ".png");
            item.setCommand("#{dockMenuBackingBean.setNewMenu}");
            //
            model.addElement(item);
            System.out.println(sp.getNome());
        }
    }

    public void setNewMenu() {
        System.out.println("A menu was clicked BUT witch menu? Arghh!!");
        //
    }

What I want to do, is to change the spSelected in ServiceProviderBackingBean, like I've done in PF3.5:

<p:dock>
    <c:forEach items="#{serviceProvidersBean.sps}" var="sp">
        <p:menuitem 
            value="#{sp.spInstanceName}" 
            icon="/images/sps/#{sp.spInstanceId}.png" 
            update=":form:spDetail" >
            <f:setPropertyActionListener 
                value="#{sp}"   
                target="#{serviceProvidersBean.spSelected}" />
        </p:menuitem>
    </c:forEach>
</p:dock>

Any help?

EDIT:

Actually I'm doing this, but I'm looking for a better and cleaner way to achieve this.

public void init() {
    if (spBean == null) {
        System.out.println("spBean is NULL!");
        return;
    }
    for (ServiceProvider sp: spBean.getListaSP()) {
        DefaultMenuItem item = new DefaultMenuItem(sp.getNome());
        //item.setUrl("#");
        item.setIcon("images/sps/" + sp.getImageId() + ".png");
        String command = String.format("#{dockMenuBackingBean.setNewMenu('%d')}", spBean.getListaSP().indexOf(sp));
        item.setCommand(command);
        //
        model.addElement(item);
        System.out.println(sp.getNome());
    }
}

public void setNewMenu(Object x) {
    Integer selectedId = Integer.parseInt((String)x);
    System.out.println("Menu changed " + Integer.toString(selectedId));
    //
}
like image 590
Daniele Vrut Avatar asked Sep 11 '13 12:09

Daniele Vrut


2 Answers

Optimus here, use setParam(key,value). You need to update to trunk code though for this.

like image 163
Cagatay Civici Avatar answered Nov 03 '22 18:11

Cagatay Civici


Setting command parameters by setParam(key,value) can be done like that:

In your menu generating bean:

DefaultMenuItem item = new DefaultMenuItem("display list");
item.setId("listMenuItem");
item.setCommand("#{myBean.displayList}");
item.setParam("listId", 1l);

In your managed bean containing the action:

public String displayList(ActionEvent event) {
    MenuItem menuItem = ((MenuActionEvent) event).getMenuItem();
    Long id = Long.parseLong(menuItem.getParams().get("listId").get(0));
    findListBy(id);
}

Reading parameters seems to be a bit complicated. But ActionListeners aren't supported by Primefaces 4 MenuItems (because they aren't derived from UICommand any more) so params seem to be new new way.

like image 26
Florian Sager Avatar answered Nov 03 '22 19:11

Florian Sager