2015-09-17 33 views
1

我試過我在Google找到的每個解決方案。似乎沒有任何工作。 問題是這樣的,我想更新表中的一行。但每次點擊更新按鈕時,我都會收到最後一行數據。我試過用地圖,試過用HtmlDataTable,還是解決不了問題。h:dataTable,HtmlDataTable不斷讀取最後一行

我使用:

<!-- JSF Dependencies --> 
<dependency> 
    <groupId>com.sun.faces</groupId> 
    <artifactId>jsf-api</artifactId> 
    <version>2.1.14</version> 
    <scope>compile</scope> 
</dependency> 
<dependency> 
    <groupId>com.sun.faces</groupId> 
    <artifactId>jsf-impl</artifactId> 
    <version>2.1.14</version> 
    <scope>compile</scope> 
</dependency> 
<dependency> 
    <groupId>javax.el</groupId> 
    <artifactId>el-api</artifactId> 
    <version>2.2</version> 
</dependency> 

我的豆:

@ManagedBean(name = "identifierManager") 
@RequestScoped 
public class IdentifiersManager { 

    private HtmlDataTable dataTable; 

    public HtmlDataTable getDataTable() { 
     return dataTable; 
    } 

    public void setDataTable(HtmlDataTable dataTable) { 
     this.dataTable = dataTable; 
    } 

    public void updateNormalIdentifier(NormalIdentifier normalIdentifier) { 
     logger.info(String.format(LOG_USER_ABOUT_TO_UPDATE_IDENTIFIER, loginController.getUserName(), normalIdentifier)); 
     // Get selected MyData item to be edited. 
     NormalIdentifier normalIdentifier_ = (NormalIdentifier) dataTable.getRowData(); 
     int index = dataTable.getRowIndex(); 
     int count = dataTable.getRowCount(); 
    } 

    //... 
}  

我的JSF頁面:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
       xmlns:ui="http://java.sun.com/jsf/facelets" 
       xmlns:f="http://java.sun.com/jsf/core" 
       xmlns:p="http://primefaces.org/ui" 
       xmlns:h="http://java.sun.com/jsf/html"> 
    <h:head> 
     <h:outputStylesheet library="css" name="table-style.css" /> 
    </h:head> 

    <h:form id="ni"> 
     <h:dataTable binding="#{identifierManager.dataTable}" 
        value="#{normalIdentifiers}" var="i" styleClass="order-table" 
        headerClass="order-table-header" 
        rowClasses="order-table-odd-row,order-table-even-row" 
        columnClasses="order-column,order-column,order-column,order-column,order-column,order-column,order-column,order-column,order-column,order-column,order-column,order-column"> 

      <h:column><f:facet name="header">User Id</f:facet>#{i.userId}</h:column> 
      <h:column><f:facet name="header">Identifier</f:facet>#{i.identifier}</h:column> 

      <h:column> 
       <f:facet name="header"></f:facet> 
       <p:commandButton id="niUpdate" 
           value="Update" 
           onclick="niConfirmationUpdate.show()" type="button" /> 
       <p:confirmDialog id="niUpdateConfirmDialog" 
           message="Are you sure you want to Update this Identifier?" 
           showEffect="drop" hideEffect="drop" header="Update Identifier" 
           severity="alert" widgetVar="niConfirmationUpdate" modal="true"> 
        <h:commandButton value="Yes" 
            oncomplete="niConfirmationUpdate.hide()" 
            actionListener="#{identifierManager.updateNormalIdentifier(i)}" 
            style="width: 80px;height: 24px;margin: 10px 5px;" 
            immediate="true" /> 
        <h:commandButton value="No" onclick="niConfirmationUpdate.hide()" 
            type="button" style="width: 80px;height: 24px" /> 
       </p:confirmDialog> 
      </h:column> 

      <h:column> 
       <f:facet name="header"></f:facet> 
       <p:commandButton id="niDelete" value="Delete" 
           onclick="niConfirmationDelete.show()" type="button" /> 
       <p:confirmDialog id="niDeleteConfirmDialog" 
           message="Are you sure you want to delete this Identifier?" 
           showEffect="drop" hideEffect="drop" header="Delete" 
           severity="alert" widgetVar="niConfirmationDelete" modal="true"> 
        <h:commandButton value="Yes" 
            oncomplete="niConfirmationDelete.hide()" 
            actionListener="#{identifierManager.removeNormalIdentifier(i)}" 
            style="width: 80px;height: 24px;margin: 10px 5px;" /> 
        <h:commandButton value="No" onclick="niConfirmationDelete.hide()" 
            type="button" style="width: 80px;height: 24px" /> 
       </p:confirmDialog> 
      </h:column> 
     </h:dataTable> 
    </h:form> 
</ui:composition> 
+0

請格式化你的代碼。您的XHTML頁面位於__一行_上。 – Tunaki

回答

1
  <p:commandButton id="niDelete" value="Delete" 
          onclick="niConfirmationDelete.show()" type="button" /> 
      <p:confirmDialog id="niDeleteConfirmDialog" 
          message="Are you sure you want to delete this Identifier?" 
          showEffect="drop" hideEffect="drop" header="Delete" 
          severity="alert" widgetVar="niConfirmationDelete" modal="true"> 

同樣的對話是通過每次重用你循環數據表。所以當然最後,對話框中的值將被綁定到最後一項。

解決方案是有動態的widgetVar。

widgetVar="myWidget-#{item.id}" 

當然並相應地更改的javascript:

PF('myWidget-#{item.id}').show; 
+0

工作,謝謝:) – snabel

+0

@snabel請接受我的答案,並放棄表示讚賞。 :一個 – Ced