0

I have dataTable for editing users.

I have commandLink in each row for opening modal dialog with selected user data for editing.

On that dialog I have commandButton for saving user.

I have a problem refreshing dataTable and displaying new edited values for user.

<p:commandButton value="Save" action="#{usersBean.updateUser()}" onclick="PF('editUserDlg').hide();" update=":adminForm:adminTabView:usersDataTable" styleClass="ui-priority-primary" style="float: right;" />

I followed BalusC answer on subject finding out id of component and have no errors in server log.

When I click on Save button I call backing bean method:

public void updateUser() {
    int userId = selectedUserView.getId();
    User user = adminSessionBean.getMe(userId);
    try {
        updateUser(user);
        LOG.info("User seccessfully updated.");
    } catch (Exception exc) {
        String message = "Error updating user: " + exc.getMessage();
        LOG.error(message);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
    }
}

and edited user is saved correctly (I've checked in database). Problem is that I don't see new edited user values in dataTable after modal dialog is closed. When I refresh page I see new values for user.

When I add call @PostConstruct init() method after updateUser(user); I see the changed values in dataTable, but I am not sure if this is the correct approach.

Community
  • 1
  • 1
vinc
  • 49
  • 7
  • Well, if your updateUsers adds it to the database, but the list that you create in your postConstruct annotated method is not updated by you, this is the behaviour you get. If you don't do any 'lazyDataModel' stuff that e.g. reads it from the db again, updating the list is something you should do. How is also up to you. Calling init() is a way, adding an explicit method to 'clear' the list and read it lazily on calling a getter is, using events/observers can be an option... just search the internet (there might even be posts on this in Stackovervlow) – Kukeltje Sep 18 '15 at 15:05
  • We used to have a very similar problem in our application. Our solution was to execute client-side code after the update, so in your case it could be something like this (after `updateUser` was called): `RequestInstance.getCurrentInstance().execute("PF('dataTableId').filter();");` (provided the data table has `widgetVar="dataTableId"`) – Sva.Mu Sep 24 '15 at 16:20

2 Answers2

0

Thanks Kukeltje, I'll go with calling init() after updateUser() method.

vinc
  • 49
  • 7
-1

I wouldn't call the init method, this method should only be called after the bean is constructed as the annotation states.

Try calling a remoteCommand instead of closing your dialog directly which refreshes the dataTable. After that you could close the dialog from that command. Something like this.

<p:remoteCommand name="updateContent" update="table"
oncomplete="PF('dialog').hide();" />

Didn't tried this solution directly, but I think that should work. I used this approach for a similar problem.

EDIT: As Kukeltje states, you also have to refresh your dataModel so that the dataTable receives the new data. You could add an actionListener to the remoteCommand to do that.

  • Why this complicated approach? There is no need at all for this. The 'update' on the save commandButton is sufficient IF the backing model/list/... is updated. Btw, where would you call this remote command from? – Kukeltje Sep 18 '15 at 15:15