Primefaces dataTable行编辑器

4

我正在使用Primefaces的p:dataTable来显示可编辑表格。有没有办法检测到p:rowEditor图标何时被点击?我需要这样做,因为我想在编辑模式下禁用一个我添加的用于删除行的p:commandLink

以下是.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>

以下是我认为与Java 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);
    }
}

}

谢谢!


链接在哪里?是在同一行内还是在表格外面?另外,请发送您的XHTML和Java代码。 - Kerem Baydoğan
@KeremBaydoğan 你好!我添加了缺失的源代码(我的错误)。删除操作的链接位于同一行中,在 p:rowEditor 旁边。 - Cristi
3个回答

9

检测 p:rowEditor 被点击的事件是:

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

在这个事件中,我如何获取被点击的编辑对象? - Wasif Tanveer

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放在另一列中。

这是一个很好的隐藏链接的想法,但是当我这样做时,删除操作不再起作用。它根本不调用bean方法。 - Cristi

1

经过大量研究,发现event="rowEditInit"是捕获可编辑行铅笔点击的正确方法。

谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接