0
votes

I have a p:menuButton in a p:column in a p:dataTable.

So far everything works fine.

Now I want to make the menuButton dynamic. However c:foreach is evaluated before rendering the table and primefaces doesn't like to use ui:repeat inside the p:menuButton.

How can I create a number of p:menuitem dynamically based on a collection on the row item I am looping over, i.e. what should I do instead of ?:someKindOfLoop in the example below?

<p:dataTable value='#{something.collection}' var='item'>
...
    <p:column>
    ...
        <p:menuButton value="Actions">
            <p:menuitem value="View ..."
              ...>
                <f:param name="something" value="#{item.id}"/>
            </p:menuitem>
            <?:someKindOfLoop x="#{item.subCollection}" y="var">
                <p:menuitem value="View sub list "
                  ...>
                    <f:param name="#{var.name}" value="#{var.id}"/>
                </p:menuitem>
            </?:someKindOfLoop>

        </p:menuButton>
    ...
    </p:column>
</p:dataTable>

Googling yields a number of similar cases, however I haven't found one yet that wasn't either about dynamically creating columns (like this: Why can <c:forEach> or <ui:repeat> not access <p:dataTable var>? ) or that doesn't deal with primefaces.

1
Only <c:forEach runs early enough to create new control items like <p:anyControl. After that, it's not permittet to change the count of control items in a page. It's possible if you use <c:forEach with runtime values, but you will get some strange results. Because your data source is a <p:... control item, there is no way to build some more. If there is a limit of possibilities, you may build always max. possible items with <c:forEach varStatus="var" and insert a rendered="#{bean.rendertest(var.index). Not nice but possible...Holger

1 Answers

2
votes

The PrimeFaces p:menuButton, like many other components, including most menu components, supports the model attribute. This is mentioned on page 329 in the PF6 documentation. The 'Dynamic menus` section of this component refers to the 'common' dynamic menus section on page 323. There you can read how to do this.

And to prevent this answer to be a 'link only' (to external sites, here is the sort of duplicate of this question: Primefaces : how to create <p:menubar> dynamically in primefaces 4?