1

I am new to SFDC and now need some help from you guys.

My requirement is: I have multiple links on VF page. Clicking any of them will pass a value to to Component and display the value on page.

enter image description here

Page: Home1

<apex:page controller="CursorTest">

    <div id="divId">
        <c:Task1 /> 
    </div>
    <apex:form >
        <apex:commandLink value="#Tab1" action="{!Save}" reRender="divId">
            <apex:param value="3" assignTo="{!eolSec}"/>
        </apex:commandLink>
        <br />
        <apex:commandLink value="#Tab2" action="{!Save}" reRender="divId">
            <apex:param value="300" assignTo="{!eolSec}"/>
        </apex:commandLink>
    </apex:form>

</apex:page>

Page Controller: CursorTest

public class CursorTest {

    public String eolSec{ get; set; }

    public CursorTest() {

    }

    public PageReference Save(){
        system.debug('--eolSec: ' +eolSec);

        Task1 t = (Task1) Task1.class.newInstance();
        if(eolSec== '3'){
            t.setTaskId('Tab1');
        }
        else if(eolSec== '300'){
            t.setTaskId('Tab2');
        }

        return null;
    }

}

Component: Task1

<apex:component controller="Task1" allowDML="true">
    <!-- <apex:attribute name="inTaskId" assignTo="{!taskId}" required="true" type="string" description="Id of task to be viewed" />
    -->

    <apex:outputtext value="Val: {!taskId}" />

</apex:component>

Component controller: Task1

public with sharing class Task1 {
    public String taskId;

    public void setTaskId(String val){
        this.taskId = val;
    }

    public String getTaskId(){
        return this.taskId;
    }



    public Task1(){
        if(taskId == null)
                taskId = '1234';
        else
            taskId = this.taskId;
    }
}

I want:

  1. Clicking on #Tab1 link, the page will show Val: Tab1
  2. Then, clicking on #Tab2 link, the page will show Val: Tab2

Please advice.

3

Try with code below. I reused your commented inTaskId attribute on your Visualforce Component, and passed the selected tab value through it. Also, my code re-renders the actual component, and not the div.

For something more advanced you can read this blog post that describes a pattern on how to implement the communication between Page Controller and the nested Visualforce Components.

Page

<apex:page controller="CursorTest">

    <c:Task1 inTaskId="{!tabId}" id="componentId"/> 

    <apex:form >
        <apex:commandLink value="#Tab1" action="{!Save}" reRender="componentId">
            <apex:param value="3" assignTo="{!eolSec}"/>
        </apex:commandLink>
        <br />
        <apex:commandLink value="#Tab2" action="{!Save}" reRender="componentId">
            <apex:param value="300" assignTo="{!eolSec}"/>
        </apex:commandLink>
    </apex:form>
</apex:page>

Page Controller

public class CursorTest {

    public String eolSec{ get; set; }
    public String tabId {get; set;}

    public PageReference Save(){
        system.debug('--eolSec: ' +eolSec);

        if(eolSec== '3'){
            tabId = 'Tab1';
        }
        else if(eolSec== '300'){
            tabId = 'Tab2';
        }

        return null;
    }

}

Component

<apex:component controller="Task1" allowDML="true">
    <apex:attribute name="inTaskId" assignTo="{!taskId}"
         required="true" type="string" 
         description="Id of task to be viewed" 
         default="1234"/>
    <apex:outputtext value="Val: {!taskId}" />
</apex:component>

Component Controller

public with sharing class Task1 {
    public String taskId {get; set;}
}
  • Mind Blowing dude :-) – Kamruzzaman Oct 23 '16 at 19:29

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.