[SalesForce] How to get a components attribute on the page and set it to a page property

I have an apex:Component and I want to get its attributes value in the page

<apex:component id="Job_Applicant_Details" controller="Job_Applicant_DetailsController">
<apex:attribute name="fName" type="String" assignTo="{!FullName}" description="Fullname of the applicant"/>

<apex:outputpanel id="panel" >
    <apex:form id="frmApplicant">
        <apex:outputlabel value="Name"/><br></br>
        <apex:inputtext id="FullName" value="{!fName}"/>
</apex:form>
</apex:outputpanel>

</apex:component>

I have a VF page and I want to set the fname attribute to the page property. I have couple of other common fields on this component and I want to access values and set it to a property on my VF page, so I can save them.

Here is my apex page

  <c:Job_Applicant_Details/>
        <apex:form >
            <apex:outputpanel >
                        <apex:outputlabel value="Skills"/>
                        <br></br>
                       <apex:inputfield id="Skills" value="{!Job_Applicant_Details__c.Skills}"/>
                        <br></br>
                   <apex:commandbutton id="btn_submit" value="Submit"/>
            </apex:outputpanel>
            </apex:form>

Its a page with a component and other input fields on the page. I want to either pass the components attributes as parameters to the page or get its value and set it to the page properties.

Best Answer

Cannot take credit for this :

How do I pass a parameter from component to a Visualforce page?

Since your component lives separated from the page any change on the component never calls the controller thereby the value of the fullname string is never set. To overcome this we use an action support to call the controller on the input field change event,

controller: ( Add your extra code over this)

public class Job_Applicant_DetailsController {
public string FullName{get;set;}
// This method is nothing but a dummy method to support the action support.
public void updateFname(){
}
}

component:

<apex:component id="Job_Applicant_Details" controller="Job_Applicant_DetailsController">
<apex:attribute name="fName" type="String" assignTo="{!FullName}" description="Fullname of the applicant"/>
<apex:outputpanel id="panel" >
    <apex:form id="frmApplicant">
        <apex:outputlabel value="Name"/><br></br>
        <apex:inputtext id="FullName" value="{!fName}">
            <apex:actionsupport event="onchange" action="{!updateFname}"/>
        </apex:inputtext>
</apex:form>
</apex:outputpanel>

Page:

<apex:page controller="Job_Applicant_DetailsController">
<c:comp_Test fName="{!FullName}"/>
Value passed from component :<b style="color:red">{!Fullname}</b> <br/>
        <apex:form >
            <apex:outputpanel >
                        <apex:outputlabel value="Skills"/>
                        <br></br>
                        <br></br>
                   <apex:commandbutton id="btn_submit" value="Submit"/>
            </apex:outputpanel>
            </apex:form>
</apex:page>

enter image description here

Related Topic