How to build a dynamic list for buttons?

by AlexSC   Last Updated January 11, 2019 12:26 PM

JSF 2.1, Primefaces 6.1, Java 7

The following piece of XHTML code builds a list of representations of uploaded files in a page:

<div>
    <p:fileUpload fileUploadListener="#{myMB.uploadFiles}" auto="true" update="uploadsPanel" label="Upload"/>
    <p:dataGrid id="uploadsPanel" value="#{myMB.uploadedFiles}" columns="5" rows="1" var="item">
        <p:column>
            <p:panel>
                <f:facet name="header">
                    <h:outputText value="#{item.name}"/>
                    <p:commandButton action="#{myMB.removeUploadedFile}" icon="fa fa-remove" update="uploadsPanel">
                        <f:param name="fileName" value="#{item.name}"/>
                    </p:commandButton>
                </f:facet>
                <h:outputText value="#{item.description}"/>
            </p:panel>
        </p:column>
    </p:dataGrid>
</div>

The idea is simple. The page allows the user to upload a number of files. After a file is uploaded, a panel is updated to show some data of each one. All this is working perfectly.

Each of the uploaded files is represented by a panel with its name, a description and a remove button. Again, everything is working.

However, the remove button only works for the very first item in the uploaded files list. If I click the remove button in the first item, the removeUploadedFile method is correctly called and the file removal works. For all the other buttons, the click does nothing.

After removing the first item, the panel is updated and the file that was the second is now the first one. The remove button will work for it, but not for the others.

What to do to make all the buttons work?

TIA!

Update 1

This is not a duplicate of some other question because:

  1. My problem has noting to with the value passed, but with the fact that the value is passed only for the first list item;
  2. The solution is not based in <f:param/>, but in <f:setPropertyActionListener>.
Tags : primefaces jsf-2


Related Questions


Determine JSF Library Resource Using Javascript

Updated August 17, 2018 16:26 PM


primefaces single project web and mobile

Updated May 18, 2015 02:11 AM