7

Apex Component: TestNavigationComponent

<apex:commandLink  onclick="return lnkClicked1();">Test Page1</apex:commandLink>
<apex:commandLink onclick="return lnkClicked2();">Test Page2</apex:commandLink>

Visualforce Pages: 2 Pages reference this component

Test Page 1

<apex:page extensions=" TestNav " standardController="Test__c">
    <c:TestNavigationComponent />
    <apex:actionFunction name=" lnkClicked1" action="{! TestNav1 }" />
    <apex:actionFunction name=" lnkClicked2" action="{! TestNav2 }" />
</apex:page>

Test Page 2

<apex:page extensions=" TestNav " standardController="Test__c">
    <c:TestNavigationComponent />
    <apex:actionFunction name=" lnkClicked1" action="{! TestNav1 }" />
    <apex:actionFunction name=" lnkClicked2" action="{! TestNav2 }" />
</apex:page>

Apex Class code:- TestNav

public pageReference TestNav1()    {
    Id id = System.currentPageReference().getParameters().get('id');       
    PageReference pageRef = Page.TestPage1;
    pageRef.getParameters().put('id',id);
    pageRef.setRedirect(true);
    return pageRef;   
}public pageReference TestNav2()
{
    Id id = System.currentPageReference().getParameters().get('id');       
    PageReference pageRef = Page.TestPage2; 
    pageRef.getParameters().put('id',id);
    pageRef.setRedirect(true);
    return pageRef;   
}

can i use only 1 action function and pass different parameters to it. If i have more than 1 links in 1 component and that component is used in different pages. I want to pass parameter as to check which link is clicked and then open page based on that

1 Answer 1

8

You can create only one action function with one parameter and reference it from the command links:

Page:

<apex:actionFunction name="lnkClicked" action="{!TestNav}" reRender="none">
    <apex:param name="p1" assignTo="{!myVar}" value=""/>
</apex:actionFunction>

Component:

<apex:commandLink onclick="lnkClicked('TestPage1');">Test Page1</apex:commandLink>
<apex:commandLink onclick="lnkClicked('TestPage2');">Test Page2</apex:commandLink>

Controller:

public String myVar { get; set; }

public pageReference TestNav(){
    PageReference pageRef = new PageReference('/apex/' + myVar);
    Id id = System.currentPageReference().getParameters().get('id');       
    pageRef.getParameters().put('id',id);
    pageRef.setRedirect(true);
    return pageRef;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .