2012-12-20 144 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”是正确的捕捉可编辑行的铅笔点击。

谢谢