Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set header of <p:dialog> in dynamically

Tags:

jsf

primefaces

I have to set head for < p : dialog> in JSF.I have written into setHeaderName() for name getter and setter.But I can not see the name of the header of < p : dialog> How to change dynamically head of p:dialog in PrimeFaces.

like image 520
emreturka Avatar asked Dec 16 '22 00:12

emreturka


2 Answers

As mentioned by mareckmareck before, you can do so by using a simple ajax update to the component.

Additionally, I would recommend to use the header fact instead the header attribute like:

<p:dialog id="someDialog" widgetVar="someDialog_var">
   <f:facet name="header">
       <h:outputText id="someDialogHeader" value="#{backingBean.dialogHeader}"/>
   </f:facet>
    ...
 </p:dialog>

And matching

<p:commandButton value="Change dialog header"
actionListener="#{someBackingBean.changeHeader}"
update="someDialogHeader"/>

(I copied and extended the example provided mareckmareck here by the way...)

This is a better solution because now you can update the headers text only, instead of the entire Dialog. (Of cause updating the entire Dialog will work with this approach too.)

Also, you may have noticed, that your dialog will close once you update the entire Dialog. This approach gets rid of that problem too.

Best Regards,

J.Adam

like image 50
Omega1001 Avatar answered Dec 25 '22 23:12

Omega1001


It strongly depends on implementation, but generally you can do it like this:

<p:dialog id="someDialog" header="#{backingBean.dialogHeader}">  
(...)
</p:dialog> 

and then change the value of field dialogHeader in the backing bean (via ajax or any other means). Remember that you need a setter and getter for this to work.

@ViewScoped
@ManagedBean
public class SomeBackingBean {

    private String dialogHeader;

    public void setDialogHeader(final String dialogHeader) { 
        this.dialogHeader = dialogHeader;
    }

    public String getDialogHeader() {
        return dialogHeader;
    }

    public void changeHeader() {
        setDialogHeader("SomeHeader");
    }
} 

Calling changeHeader method and rerendering dialog will change the header. For example it could be called like this:

<p:commandButton value="Change dialog header"
    actionListener="#{someBackingBean.changeHeader}"
    update="someDialog"/>           
like image 37
mareckmareck Avatar answered Dec 25 '22 22:12

mareckmareck