Note that there are some explanatory texts on larger screens.

plurals
  1. POPrimeFaces 3.5: row-specific context menu in datatable
    primarykey
    data
    text
    <p>I'm working on a web application using JSF and PrimeFaces. One of the pages displays a table of entities, and I'm supposed to add a context menu to the table. </p> <p>More specifically: The last column contains a <code>p:menuButton</code> whose content depends on the respective row. The menuItems are stored as a <code>ui:composition</code>in another file, and I'm supposed to simply include them in the context menu.</p> <p>The menuitems are dynamic, however. The entities do have a boolean member called enabled. For enabled entities, Edit, Delete, and Disable are displayed. For disabled entities, there's only Enable.</p> <p>With the code below, the context menu is displayed, but they the content is only updated if I select another row via left click. Right-clicking does select rows, but the selectedPerson of the backingbean is not updated.</p> <p>Apparently there were some changes in PrimeFaces 3.5. I found many references to the contextMenu ajax event.</p> <p>But when I add it to the datatable like this:</p> <pre><code>&lt;p:ajax event="contextMenu" update=":persons-form:contextMenu" /&gt; </code></pre> <p>.. the menu appears but disappears immediately. Presumably, because some update method is getting called. In the short timespan it's displayed, I can see it still showing the old menuitems.</p> <p><strong>So, to summarize: How can I make a contextmenu working as it should in a datatable with PrimeFaces 3.5? Meaning, right-clicking should select a row and also update the menu before displaying it, as the menuitems depend on the row data.</strong></p> <p>​ ​</p> <p><strong>personList.xhtml:</strong></p> <p> </p> <pre><code> &lt;p:contextMenu id="contextMenu" for="persons"&gt; &lt;ui:include src="/layout/personActionMenu.xhtml"&gt; &lt;ui:param name="thePerson" value="#{personListBean.selectedPerson}"/&gt; &lt;/ui:include&gt; &lt;/p:contextMenu&gt; &lt;p:dataTable id="persons" value="#{personListBean.allPersons}" var="p" rowKey="#{p.id}" selection="#{personListBean.selectedPerson}" selectionMode="single" rowStyleClass="#{p.enabled ? '' : 'disabledTableItem'}"&gt; &lt;p:ajax event="rowSelect" update=":persons-form:contextMenu"/&gt; &lt;p:column&gt; &lt;f:facet name="header"&gt;Name&lt;/f:facet&gt; &lt;h:outputText value="#{p.name}" /&gt; &lt;/p:column&gt; &lt;p:column&gt; &lt;p:menuButton value="Actions"&gt; &lt;ui:include src="/layout/personActionMenu.xhtml"&gt; &lt;ui:param name="thePerson" value="#{p}"/&gt; &lt;/ui:include&gt; &lt;/p:menuButton&gt; &lt;/p:column&gt; &lt;/p:dataTable&gt; &lt;/h:form&gt; </code></pre> <p><strong>personActionMenu.xhtml:</strong></p> <pre><code>&lt;ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:u="http://java.sun.com/jsf/composite/ui" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"&gt; &lt;p:menuitem rendered="#{personListBean.isEnabled(thePerson)}" outcome="edit.xhtml" value="Edit" icon="ui-icon-gear"&gt; &lt;f:param name="id" value="#{thePerson.id}" /&gt; &lt;/p:menuitem&gt; &lt;p:menuitem actionListener="#{personListBean.deletePerson(thePerson)}" value="Delete" update=":person-form" icon="ui-icon-close" rendered="#{personListBean.isEnabled(thePerson)}"/&gt; &lt;p:menuitem actionListener="#{personListBean.toggleEnabled(thePerson)}" update=":persons-form" value="#{personListBean.toggleEnabledMenuItemLabel(thePerson)}" /&gt; &lt;/ui:composition&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload