2

I have component that load list of items and the user clicks on the hyperlink it will pass along the id of the record user clicked to page controller.

I could not able to figured out how to achieve this, here is my source code.

Component page (EmployeeComponent):

<apex:repeat value="{!Employees}" var="e" id="r">
    <apex:commandLink value="{!e.Name}" action="{!clickOnName}">
        <apex:param name="eId" value="{!e.Id}" assignTo="{!empId}"/>
    </apex:commandLink>
</apex:repeat>

Component Controller:

public Id empId{get; set;}

public PageReference clickOnName() 
{ 
    PageReference newPage = Page.NewEmployee; 
    newPage.getParameters().put('empId', empId);  
    return newPage.setRedirect(true); 
} 

How do I get access to this getter (empId) in the page Controller?

VF page:

<c:EmployeeComponent></c:EmployeeComponent>
3

Based on your comment below....

Values are passed by reference so you could do the following:

You do not have to use the same controller but in this example I did.

I essentially passed the value I want to set to the component and then has the command link assign the selected value to the attribute that was pointing to the value property passed from the main vf page.

The component then re renders the block on the main page to show that the value was set

Class

public class myExampleClass{

    public Id empId{get; set;}
    public Id selectedId {get;set;}

    public myExampleClass(){}
    public myExampleClass(ApexPages.StandardController con){}

    public void clickOnName() 
    { 

    } 

}

Component

<apex:component controller="myExampleClass">

<apex:attribute name="con" type="Contact[]" required="true" description="the contacts"/>
<apex:attribute name="val" type="Id" required="true" description="value to set"/>
    <apex:form>
        <apex:repeat value="{!con}" var="e" id="r">
            <apex:commandLink value="{!e.Name}" reRender="myBlock">
                <apex:param name="eId" value="{!e.Id}" assignTo="{!val}"/>
            </apex:commandLink>
        </apex:repeat>
    </apex:form>


</apex:component>

Page

<apex:page standardController="Account" extensions="myExampleClass">

    <c:MyTestComponent con="{!Account.Contacts}" val="{!selectedId}"/>


    <apex:outPutPanel layout="block" id="myBlock">
        {!selectedId}
    </apex:outPutPanel>
</apex:page>

Another way would be to pass the controller to the component as indicated here: How do I pass a parameter from component to a Visualforce page?

We may need a bit more information from your code since both in VF page standalone and as a component as the below works just fine using your code...There may be some things that you have not defined but without the full example it is hard to say.

Use the below as a guid and work from there adding stuff you need and see what breaks it

Standalone VF Page

Class

public class myExampleClass{

public Id empId{get; set;}

    public myExampleClass(ApexPages.StandardController con){}

    public PageReference clickOnName() 
    { 
        PageReference newPage = Page.DisplayId; 
        newPage.getParameters().put('empId', empId);  
        return newPage.setRedirect(true); 
    } 

    }

First VF Page

<apex:page standardController="Account" extensions="myExampleClass">

    <apex:form>
        <apex:repeat value="{!Account.Contacts}" var="e" id="r">
            <apex:commandLink value="{!e.Name}" action="{!clickOnName}">
                <apex:param name="eId" value="{!e.Id}" assignTo="{!empId}"/>
            </apex:commandLink>
        </apex:repeat>
    </apex:form>
</apex:page>

Second VF Page

<apex:page >
    {!$CurrentPage.parameters.empId}
</apex:page>

Using a component (and second VF page above) as follows:

Component

<apex:component controller="myExampleClass">

<apex:attribute name="con" type="Contact[]" required="true" description="the contacts"/>
    <apex:form>
        <apex:repeat value="{!con}" var="e" id="r">
            <apex:commandLink value="{!e.Name}" action="{!clickOnName}">
                <apex:param name="eId" value="{!e.Id}" assignTo="{!empId}"/>
            </apex:commandLink>
        </apex:repeat>
    </apex:form>


</apex:component>

Controller

public class myExampleClass{

    public Id empId{get; set;}

    public myExampleClass(){}

    public PageReference clickOnName() 
    { 
        PageReference newPage = Page.DisplayId; 
        newPage.getParameters().put('empId', empId);  
        return newPage.setRedirect(true); 
    } 

}

Page using Component

<apex:page standardController="Account">

    <c:MyTestComponent con="{!Account.Contacts}"/>

</apex:page>

the above examples essentially uses your code without modification so it proves that it works as expected...

  • His code is in component which you have tried to use in First VF page – Santanu Boral Jan 28 '17 at 23:02
  • @SantanuBoral - I was updating with component example as well. See updated answer – Eric Jan 28 '17 at 23:03
  • @Eric: I just need an Id to be passed back from component to page controller, i'm doing all my soql in the component controller so i'm not passing anything from vf page as shown in my question. – Nick Kahn Jan 28 '17 at 23:08
  • @NickKahn how did it go? – Eric Jan 29 '17 at 8:29
  • @Eric: 1+ As I was going through your code and I noticed that you are using standardController and Extensions is the component ControllerClass but I'm using the CustomObject I'm just curious to know and understand, is there any blog or site where it talk detail about the technic that you have posted? – Nick Kahn Jan 30 '17 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.