Search code examples
datatableicefacesparam

ICE Faces - sending filename param to bean from ice:outputResource


I have acefaces datatable with file names, one on row and I'm trying to add a download button using ice:outputResource, but I fails in sending the filename to outputResourceBean.

My datatable looks like:

<h:form id="exportedFiles">
       <ace:dataTable value="#{exportBean.allFiles}" var="upload" 

                    <ace:column>
                        <f:facet name="header" >
                            <h:outputText value="Název souboru"/>
                        </f:facet>    
                        <h:outputText value="#{upload.name}"/>
                    </ace:column>

                    <ace:column>
                        <f:facet name="header" >
                            <h:outputText value="Dowload file"/>
                        </f:facet>      
                        <ice:outputResource id="downLink" 
                                            resource="#{outputResource.pdfResource}"
                                            attachment="true"
                                            label="Download PDF"
                                            type="button">
                        </ice:outputResource>
                    </ace:column>
                </ace:dataTable>
            </h:form>

And my bean look like this:

public static final String PDF_NAME = "Download_PDF.pdf";
public static final Resource PDF_RESOURCE = new MyResource(PDF_NAME);
public String getPdfName() { return PDF_NAME; }
public Resource getPdfResource() { return PDF_RESOURCE; }

I can get the filename using #{upload.name} but I have no idea how to send it to bean when clicking the Download button...

Thanks for help in advance!


Solution

  • You can wrap your list containing the files into a ListDataModel. That allows you to access the data of the current selected row index.

    @ManagedBean
    @ViewScoped
    public class ExportBean {
    
        private ListDataModel<MyResource> allFiles;
    
        public ListDataModel<MyResource> getAllFiles() {
            if (allFiles == null) {
                List<MyResource> files = new ArrayList<MyResource>();
                files.add(new MyResource("doc1.pdf"));
                files.add(new MyResource("doc2.pdf"));
                files.add(new MyResource("doc3.pdf"));
                files.add(new MyResource("doc4.pdf"));
                files.add(new MyResource("doc5.pdf"));
                allFiles = new ListDataModel<MyResource>(files);
            }
            return allFiles;
        }
    
        public String getPdfName() {
            // add the getResourceName method to MyResource
            return allFiles.getRowData().getResourceName();
        }
    
        public Resource getPdfResource() {
            return allFiles.getRowData();
        }
    }
    

    And the xhtml page would look like this:

    <h:form id="exportedFiles">
        <ace:dataTable value="#{exportBean.allFiles}">
            <ace:column>
                <h:outputText value="#{exportBean.pdfName}" />
            </ace:column>
            <ace:column>
                <ice:outputResource resource="#{exportBean.pdfResource}" attachment="true" label="Download PDF" type="button" />
            </ace:column>
        </ace:dataTable>
    </h:form>