简体   繁体   中英

JSF Primefaces Datatable selectionMode for without unique id

I am working on a browser based ftp client. I am using org.apache.commons.net.ftp.FTPFile 3.6 and Primefaces 6.2. For the visual representation of actual file context I am using the DataTable component of PF. Now I have come across the following problem/challenge. As i want to select files and execute certain actions on double click I am enabling the selectionMode. This is the jsf code

<p:dataTable id="ftptable" var="file" value="#{fTPBean.actualFolderContent}" `selectionMode="single" selection="#{fTPBean.selectedFile}" rowKey="#{file}>
<p:column headerText="File/Folder">     
    <h:outputText value="#{file.name}" />
</p:column>

<p:column headerText="File Info">
    <h:outputText value="#{file.rawListing}" />
</p:column>
</p:/dataTable>

This will not work, as a rowKey is expected to be a unique identifier and org.apache.commons.net.ftp.FTPFile is not something coming out of a database or not designed by me. Actually my goal is to allow files/folders to be selected and execute actions on click resp. double-click.

Is there no way around this with dataTable or am I doing things completely wrong here. In that case you are welcome to -1 me, but a reason is very welcome;)!

You can always make a wrapper class for org.apache.commons.net.ftp.FTPFile which should be identifiable and contains FTPFile instance. In that case you can simply use a collection of this wrappers, to represent them in the DataTable and perfom an action on them.

public class FTPFileWrapper implements Serializable {

    private static final long serialVersionUID = 3653846184126846410L;

    private Integer id;

    private FTPFile ftpFile;

    public FTPFileWrapper(Integer id, FTPFile ftpFile) {
        this.id = id;
        this.ftpFile = ftpFile;
    }

    //getters and setters
}

And you need to make a fTPBean.actualFolderContent collection of the FTPFileWrapper and populate if with some unique id value (it can be just a simple int variable incrementation) and FTPFile you want to show in your UI.

And to show the name of your file you can access it via file as follows:

<p:column headerText="File/Folder">     
    <h:outputText value="#{file.ftpFile.name}" />
</p:column>

or modify the wrapper, to access it's file fields directly as follows:

public class FTPFileWrapper implements Serializable {

    ...

    public String getFileName() {
        if (ftpFile != null) {
            return ftpFile.getName();
        }
        return "N/A";
    }
}

and

<p:column headerText="File/Folder">     
    <h:outputText value="#{file.fileName}" />
</p:column>

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