Chandan Sarma
Chandan Sarma

Reputation: 308

How to copy a text field in PrimeFaces using p:ajax?

Hi In my application i have two block one in present Address and other one permanent Address.

If My present address and permanent address is same i need to copy the present address field to permanent Address field.

I have attached the code here

<h:outputText value="#{msg['elicense.examinationform.address.presentaddressline1']}" />
<p:inputText id="presentaddress1" value="#{personalBean.presentAddressLine1}" label="Present Address Line1" />
<p:watermark for="presentaddress1" value="Present Address Line1" id="presentaddressdata1"></p:watermark>


<h:outputText value="#{msg['elicense.examinationform.address.copy']}" />
<p:selectBooleanCheckbox value="#{examinationFormBean.copyAddress}" label="addresscopy" />
<p:spacer></p:spacer>

<h:outputText value="#{msg['elicense.examinationform.address.permanentline1']}" />
<p:inputText id="address1" value="#{personalBean.permanentAddressLine1}" label="Permanent Address Line1" />

I have updated my code. I need to copy presentAddressLine1 value to permanentAddressLine1 when ever i checked the check box using p:ajax. please help me

Upvotes: 1

Views: 1875

Answers (1)

Aritz
Aritz

Reputation: 31669

That's a matter of sending an Ajax request when the p:selectBooleanCheckBox component gets clicked. Here you've got a basic SSCCE:

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private String address1;
    private String address2;
    private boolean copyAdress;

    public String getAddress1() {
        return address1;
    }

    public String getAddress2() {
        return address2;
    }

    public boolean isCopyAdress() {
        return copyAdress;
    }

    public void listener() {
        if (copyAdress) {
            address2 = address1;
        } else {
            address2 = "";
        }
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public void setCopyAdress(boolean copyAdress) {
        this.copyAdress = copyAdress;
    }

}
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
    <h:form>
        <p:inputText value="#{bean.address1}" />
        <p:inputText id="add2" value="#{bean.address2}" />
        <p:selectBooleanCheckbox value="#{bean.copyAdress}">
            <p:ajax listener="#{bean.listener}" update="add2" process="@form" />
        </p:selectBooleanCheckbox>
    </h:form>
</h:body>
</html>

The p:ajax specifies a listener with the method we want to invoke in server side. We'll send the whole form to process (that way the current values for the addresses are set before the listener being executed) and the component to update after the request must be the input attached to the second address, which is the one we want to copy the value to.

Upvotes: 1

Related Questions