0

I'm new to PrimeFaces (and JSF too) and I'm trying to update dialog message based on retrieved data.

View looks like:

    <p:dialog id="userDialog" header="Confirmation" widgetVar="userDialog" dynamic="true"  modal="true" closable="fasle" width="680px" resizable="false">
    <h:form>
        <b>Email address #{userListView.email}</b> is already registered with the following user <b>#{userListView.firstName}</b> <b>#{userListView.lasttName}</b>:

        <b>Please confirm that you would like to proceed with adding an additional account for this User.</b><br />
        <hr />
        <div class="ui-confirm-dialog-footer">
            <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="pi pi-check" onclick="PF('userDialog').hide()" />
            <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="pi pi-times" onclick="PF('userDialog').hide()" />
        </div>
    </h:form>

And controller looks like:

@ManagedBean(name = "userListView")
@ViewScoped
public class UserListView extends DataTableListView implements Serializable {

 .....some other code...

    public String getFirstName() {

       String firstName = "";

       List<UserMembership> membershipList = getMembershipList();

       if (!membershipList.isEmpty()) {
           UserMembership membership = membershipList.get(0);
           firstName = membership.getUser().getFirstName();
       }

       return firstName;
    }

    public String getLasttName() {

       String lastName = "";

       List<UserMembership> membershipList = getMembershipList();

       if (!membershipList.isEmpty()) {
           UserMembership membership = membershipList.get(0);
           lastName = membership.getUser().getLastName();
       }

       return lastName;
    }

    public String getEmail() {

       String email = "";

       List<UserMembership> membershipList = getMembershipList();

        if (!membershipList.isEmpty()) {
            UserMembership membership = membershipList.get(0);
            email = membership.getUser().getEmail();
        }

        return email;
    }

 ....some other code...
}

and dialog box is spawned as

RequestContext.getCurrentInstance().execute("showDialog(userDialog')");

It will work fine for the first user but then when I will get that dialog box for another one it keeps data from first one not updated with current.

Also I have try to add

onHide="PF('userDialog').content.empty()"

to the dialog but that will remove whole dialog box content.

How can I destruct and refresh that dialog box with new data then?

PS. Not sure if I need to get this done via

   @PostConstruct
   public void init() {}
JackTheKnife
  • 3,795
  • 8
  • 57
  • 117
  • 1
    Are you new to JSF as well? – Kukeltje Jun 14 '19 at 14:30
  • @Kukeltje yeah - I'm PHP guy :D and welcome again! – JackTheKnife Jun 14 '19 at 14:30
  • 1
    There are several ways, but most importantly, it needs to be clear when and where you initiate the updating of the backing data? But start reading https://stackoverflow.com/questions/11365094/can-i-update-a-jsf-component-from-a-jsf-backing-bean-method – Kukeltje Jun 14 '19 at 14:38
  • OK. I have tried to use `RequestContext.getCurrentInstance().update("userDialog");` just after `RequestContext.getCurrentInstance().execute("showDialog('userDialog')");` but that throws `ComponentNotFoundException` – JackTheKnife Jun 14 '19 at 14:52
  • 1
    Then that is not the **full client id** of the component (and for which there are multiple Q/A in stackoverflow. – Kukeltje Jun 14 '19 at 15:05

2 Answers2

2

In the case you are using Primefaces 7.0+ swap the already mentioned

RequestContext.getCurrentInstance().update("userDialog");

for

PrimeFaces.current().ajax().update("userDialog");

If I can suggest you, set some id for <h:form> and use the full client ID after, something like

PrimeFaces.current().ajax().update("userDialog:yourFormId");

BalusC already answer a similar question in this thread Can I update a JSF component from a JSF backing bean method?

  • You **need** the id of the form (if there is one around the dialog), otherwise it won't work. The reference is always from the 'root'. But in this case the form is inside the dialog – Kukeltje Jun 14 '19 at 17:40
  • Yes I know, the answer was for his case. Actually, if I'm not wrong, a form inside dialog it's ok. In scenarios where you use appendTo, he actually need to be inside. – Roger Alves Jun 14 '19 at 18:10
  • Is there a way to update and then show instead having two lines which in Google Chrome sometimes will open `userDialog` twice one after another? – JackTheKnife Jun 17 '19 at 20:24
  • I'm not advanced JSF/Primefaces developer, but I guess you can't. But you can safely use PrimeFaces.current().executeScript("PF('userDialog').show()") and use the update after, the correct behavior doesn't show two dialog (remove onclick from the xhtml file, now your bean will show the dialog). – Roger Alves Jun 18 '19 at 02:30
  • in case of old Primefaces version, just use the already known RequestContext.getCurrentInstance().execute("PF('userDialog').show()"); – Roger Alves Jun 18 '19 at 02:35
0

In my case fix looks like

RequestContext.getCurrentInstance().update(":userDialog");

just after .execute() part.

JackTheKnife
  • 3,795
  • 8
  • 57
  • 117
  • Sure it does not wor without the colon? From my understanding it is automatically absolute (which is what the colon does) – Kukeltje Jun 14 '19 at 17:40