通过非ajax按钮发布请求获取Primefaces数据表行变量

问题描述:

我在我的Primefaces页面中有一个数据表和轮询。在每行的Datatable上都有一个commanButton。由于轮询,f:setPropertyActionListener在按钮单击时无法正常工作。所以我在button上设置了ajax = false,并试图通过POST请求获取数据行“var”。有没有办法做到这一点?通过非ajax按钮发布请求获取Primefaces数据表行变量

<p:poll interval="15" 
     listener="#{batchOperation.generateImportFTDBatchFileReport}" 
     update=":batchOperationsForm:growl :batchOperationsForm:batchfilestbl    
       :batchOperationsForm:dataimporttypeselectiontabview:importFTDbatchfilestbl 
       :batchOperationsForm:dataimporttypeselectiontabview:importFTDerrorbatchfilestbl 
       :batchOperationsForm:dataimporttypeselectiontabview:importFTDStatusTxtId" 
     widgetVar="myPoll" autoStart="true"/> 

<p:dataTable id="batchfilestbl" var="batchFile" 
        value="#{batchOperation.batchFileModel}" paginator="true" 
        rows="#{batchOperation.batchFileModel.pageSize}" 
        paginatorPosition="bottom" 
        sortBy="#{batchOperation.createTime}" 
        sortOrder="descending" 
        emptyMessage="#{messages['common.datatable.emptymessage']}" 
        selectionMode="single" 
        selection="#{batchOperation.selectedFile}"> 


    <p:column headerText="#{messages['content.batchoperations.datatable.header']}"> 

      <p:commandButton actionListener="#{batchOperation.createBatchFileForDownloadLatestRevision}" 
         id="excelCreate" disabled="#{disableExcelVar}" 
         value="#{messages['content.batchoperations.createexcel.button.label']}" 
         ajax="false"> 
         <f:setPropertyActionListener value="#{batchFile}" 
          target="#{batchOperation.selectedFile}" /> 
      </p:commandButton> 
    </p:column> 
</p:dataTable> 

而不是使用f:setPropertyActionListener,f:属性解决了我的问题。

  <p:commandButton actionListener="#{batchOperation.createBatchFileForDownloadLatestRevision}" 
          id="excelCreate" disabled="#{disableExcelVar}" 
          value="#{messages['content.batchoperations.createexcel.button.label']}" 
          ajax="false"> 
       <f:attribute name="bf" value="#{batchFile}" /> 
     </p:commandButton> 

方法:

 public synchronized void createBatchFileForDownloadLatestRevision(ActionEvent event) throws Exception { 

      selectedFile = (BatchFile) ((CommandButton) event.getSource()).getAttributes().get("bf"); 

      . 
      . 
     } 

如果您想发送整个模型对象作为参数到后台bean的方法,您可以使用您的var为数据表发送。

<p:dataTable var="myVar"> 
    <p:column> 
     <p:commandButton actionListener="#{bean.method(myVar)}" /> 
    </p:column> 
</p:dataTable> 

另外,如果你只对行索引有兴趣,你可以只是使用数据表中rowIndexVar值发送。

<p:dataTable rowIndexVar="rowIndex"> 
    <p:column> 
     <p:commandButton actionListener="#{bean.method(rowIndex)}" /> 
    </p:column> 
</p:dataTable> 

它看起来像你试图在这里做文件下载。您可能想要结账this的问题。

+0

感谢的建议,米切尔。不幸的是,它不适合我,也许因为JSF/Primefaces版本。我找到了一个替代解决方案,并在下面发布。 –