1

How to make a communication from vf's custom component to my vf page's controller apex class.

I'm trying to wrap my head around communicating between vf componenet/controller and i might be close or way out, can anyone correct me what I'm doing wrong here?

This is what I have done with no success so far.

VF component:

<apex:component controller="ComponentController">
    <apex:attribute type="selectedAccount" name="userSelectedAccount" description="Selected Account" assignTo="{!selectedAccount}"/>
</apex:component>

VF component controller:

public class ComponentController {
    public selectedAccount {get;set;}    
    public void ComponentController()
    {
       list<account> acc = [select id, name from account limit 1];
       selectedAccount.theAccount = acc[0];
    }    
}

VF page:

<apex:page controller="pagecontroller" >
    <c:mycomp userSelectedAccount="{!instanceOfSelectedAccount}"/>
</apex:page>

VF page controller:

public class pagecontroller {    
    public selectedaccount instanceOfSelectedAccount {get;set;}
    public pagecontroller() {
        instanceOfSelectedAccount = new selectedaccount();    
    }
}

class:

public with sharing class selectedAccount 
{
    public account theAccount {get;set;} 
}
2
  • Are you trying to set the account in the component, and then pass that same account back up to the visualforce controller? Dec 7, 2016 at 20:41
  • the above code is just poc so what i'm trying to do is get the account from component to vf controller
    – Nick
    Dec 7, 2016 at 20:43

1 Answer 1

1

I wrote an example based on slightly abusing apex:actionFunction. Since the component isn't in an iFrame, any javascript functions attached to the window are accessible from the component, even if they aren't defined in the component.

This means that we can pass a value through javascript from the component, into the controller, by knowing the name of the javascript function used, and having a param with an assignTo value of a variable in the page controller.

This example defines a function to set the current account Id in the visualforce controller, and then calls this function as a javascript function in the component with a randomly assigned id, which gets passed to the controller, and sets the account id in the controller.

Page Controller

public class Test_controller {

    public Id AccountID { get; set; }
    public Account account { get; set; }

    public Test_controller() {}

    public PageReference SetCurrentAccount() {
        account = [SELECT Id, Name FROM Account WHERE Id = :AccountID];

        return null;
    }
}

Page

<apex:page controller="Test_controller">

    <apex:form>
        <apex:actionFunction name="SetCurrentAccount" action="{!SetCurrentAccount}" reRender="somePanel">
            <apex:param name="accountId" value="" assignTo="{!AccountID}" />
        </apex:actionfunction>

        <apex:outputPanel id="somePanel">
            {!AccountID}
        </apex:outputpanel>

        <c:TestingComponent />

    </apex:form>

</apex:page>

Component

<apex:component controller="TestingComponent_controller">
    <script>
        setTimeout(SetCurrentAccount, 500, '{!SomeId}');
    </script>
</apex:component>

Component Controller

public class TestingComponent_controller {

    public ID someId {
        get {
            if (someId == null) {
                someId = [SELECT ID FROM Account LIMIT 1].Id;
            }

            return someId;
        }
        set;
    }

    public TestingComponent_controller() {}
}
0

You must log in to answer this question.

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