简体   繁体   中英

How to use contextMenu with ContentFlow in Primefaces

Sorry in advance fro lame question, I'm still new to JSF/Primefaces. I'd like to use ContextMenu with ContentFlow. For example, when user right-clicks on the ContentFlow control I would like to display the context menu with actions like Delete, Add New etc. My code in JSF page looks like this:

<p:contentFlow id="productpictures" 
               value="#{myviewscopedbean.productpictures}" 
               var="picture" >
     <p:graphicImage url="#{picture}" styleClass="content"/>
</p:contentFlow> 
<p:contextMenu  for="productpictures" >
     <p:menuitem value="Refresh" update="productpictures" />
     <p:separator/>
     <p:menuitem value="Add New" onclick="PF('dlgcreate').show()" />
     <p:menuitem value="Delete" actionListener="#{myviewscopedbean.removePicture}"
                                update="productpictures" />
</p:contextMenu>

The problem I'm fighting with - how to determine the 'active' image in the contentflow and how to pass it to myviewscopedbean.removePicture() method.

I found some hints here but still confused - please help!

You could use p:remoteCommand and JQuery to get the image and read image from the image caption. The code below gives caption as selected image.

Your modified code

   <p:contentFlow id="productpictures" 
                   value="#{myviewscopedbean.productpictures}" 
                   var="picture" >
         <p:graphicImage url="#{picture}" styleClass="content"/>
         <div class="caption">#{picture}</div>
    </p:contentFlow> 
    <p:contextMenu  for="productpictures" >
         <p:menuitem value="Refresh" update="productpictures" />
         <p:separator/>
         <p:menuitem value="Add New" onclick="PF('dlgcreate').show()" />
         <p:menuitem value="Delete" oncomplete="removePicture([{name:'selectedImage', value: $('.item.active div.caption').text()}]);"
                                     />
    </p:contextMenu>

<p:remoteCommand name="removePicture" actionListener="#{myviewscopedbean.removePicture}" update="productpictures"/>

Java

@ViewScoped('myviewscopedbean')
class MyBean{
    public void removePicture(){
       Map<String, String[]> paramValues = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap();
       String[] selectedImages = paramValues.get("selectedImage");        
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM