Getting currently iterated data table row in p:fil

2020-04-18 07:04发布

问题:

I'm using <p:fileUpload> in a <p:dataTable>. Upload works fine, but I'd like to know the currently iterated row in listener method so that I can update the right row in my DB.

XHTML:

<p:fileUpload fileUploadListener="#{doorBean.handleFileUpload}"
    mode="advanced" dragDropSupport="false" update="messages"
    sizeLimit="3000000"
    allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />

Bean:

public void handleFileUpload(FileUploadEvent event) {
    this.file = event.getFile();
    ...
}

How can I achieve this? Can I pass an additional parameter to the bean?

回答1:

Given a

<p:dataTable value="#{bean.items}" var="item" ...>

either grab the currently iterated row from EL in the listener method:

FacesContext context = FacesContext.getCurrentInstance();
Item item = context.getApplication().evaluateExpressionGet(context, "#{item}", Item.class);

or simply move the listener method from the Bean to the Item:

 <p:fileUpload fileUploadListener="#{item.handleFileUpload}" ... />