2012-12-20 118 views
3

我正在使用Primefaces的p:dataTable來顯示可編輯的表格。有沒有什麼辦法可以檢測到p:rowEditor圖標被點擊的時間?我需要這個,因爲我想在編輯模式下禁用我爲行刪除添加的p:commandLinkPrimefaces dataTable rowEditor

而這裏的.xhtml:

<p:dataTable paginatorAlwaysVisible="true" 
       paginator="true" 
       paginatorPosition="top" 
       paginatorTemplate="{CurrentPageReport} {PageLinks} {RowsPerPageDropdown}" 
       rowsPerPageTemplate="10,25,50" 
       rows="10" 
       editable="true" 
       value="#{userController.allUsers}" 
       var="user" 
       > 
     <p:ajax event="rowEdit" listener="#{userController.onEdit}"/> 
     <p:column headerText="First Name"> 
      <p:cellEditor> 
       <f:facet name="output"> 
        <h:outputText value="#{user.firstname}"/> 
       </f:facet> 
       <f:facet name="input"> 
        <h:inputText value="#{user.firstname}"/> 
       </f:facet> 
      </p:cellEditor> 
     </p:column> 

     //. . . some other data columns 

     <p:column headerText="Options"> 
      <p:rowEditor/> <br/> 
      <p:commandLink id="deleteLink" styleClass="ui-icon ui-icon-trash" action="#{userController.deleteUser(user.userId)}"/> 
     </p:column> 

這裏bean的部分我找到相關:

@ManagedBean 
@SessionScoped 
public class UserController { 
    @EJB 
    private UserBean userBean; 
    @EJB 
    private TeamBean teamBean; 
    private Integer currentUserId; 
    private String newUserUsername; 
    private String newUserPassword; 
    private User.AccountType newUserAccountType; 
    private String newUserFirstName; 
    private String newUserLastName; 
    private Integer newUserTeamId; 

    // ... some create/ update/ delete functions that work 

    public void onEdit(RowEditEvent event) { 
     try { 
      User user = (User) event.getObject(); 
      System.out.println("Edit: " + user); 

      userBean.update(user.getUserId(), user.getUsername(), user.getPassword(), 
       User.AccountType.valueOf(user.getAccountType()), user.getFirstname(), user.getLastname(), 
       user.getTeam() == null ? null : user.getTeam().getTeamId()); 
      System.out.println("User " + user.getUserId() + " updated: " + user.getFirstname()); 
    } catch (InexistentUserException ex) { 
     Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (InvalidUsernameException ex) { 
     Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (InexistentTeamException ex) { 
     Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (DataBaseException ex) { 
     Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

} 

謝謝!

+0

哪裏有鏈接?在同一行內還是在表外?另請發送您的xhtml和java請 –

+0

@KeremBaydoğan嗨!我添加了缺少的源代碼(我的錯誤)。刪除操作的鏈接位於「p:rowEditor」旁邊的同一行內。 – Cristi

回答

7

事件檢測與p時:rowEditor被點擊的是:

<p:ajax event="rowEditInit" listener="#{Bean.someListener}" /> 
+0

它這個事件如何獲得點擊編輯的對象? –

3

只需在列中使用另一個<p:cellEditor>即可。

<p:column headerText="Options"> 
    <p:rowEditor/> 
    <p:cellEditor> 
    <f:facet name="output"> 
     <p:commandLink id="deleteLink" styleClass="ui-icon ui-icon-trash" action="#{userController.deleteUser(user.userId)}"/> 
    </f:facet> 
    <f:facet name="input"> 

    </f:facet> 
    </p:cellEditor> 
</p:column> 

我也建議你把deleteLink另一列內。

+0

這是隱藏鏈接的好主意,但是當我這樣做時,刪除操作不再有效。它根本不調用bean方法。 – Cristi

1

經過大量研究事件=「rowEditInit」是正確的捕捉可編輯行的鉛筆點擊。

謝謝