Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces dataTable rowEditor

Tags:

jsf

primefaces

I'm using Primefaces' p:dataTable to display an editable table. Is there any way to detect when the p:rowEditor icon has been clicked? I need this because i want to disable, when in editing mode, a p:commandLink that i've added for row deletion.

And here's the .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>

And here the parts of the bean i find relevant:

@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);
    }
}

}

Thanks!

like image 908
Cristi Avatar asked Dec 11 '22 19:12

Cristi


2 Answers

The event to detect when the p:rowEditor was clicked is:

<p:ajax event="rowEditInit" listener="#{Bean.someListener}" />
like image 50
Juan Trejos Avatar answered Dec 26 '22 03:12

Juan Trejos


Just use another <p:cellEditor> inside your column.

<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>

I also recommend you to place deleteLink inside another column.

like image 42
Kerem Baydoğan Avatar answered Dec 26 '22 03:12

Kerem Baydoğan