How to get a value from a user controller of a component on a Visualforce page controller?

I am trying to create a custom visualforce component that is an entity. This user component displays a user interface that helps you view some entries. You can select one entry, and I would like to get it from outside the component or its controller.

I looked at the standard Salesforce binding with assignTo, but this is not bi-directional ...

Hope someone can help me .. Thanks

+4
source share
3 answers

Are you passing an object to a component? Objects are passed by reference, so if your component has an attribute that takes an object and does something with it, your external page editor will be able to access the changed values.

If you were to pass a wrapper object, i.e. if your user interface allows the user to select an account.

Class SelectedAccount { public Account theAccount {get;set;} } 

component:

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

Component Controller:

 public class ComponentController { public selectedAccount; public void ComponentController(){} public PageReference selectAccountFromUI(Account selected) { selectedAccount.theAccount = selected; return null; } } 

Component Usage Page:

 <c:MyAccountComponent userSelectedAccount="{!instanceOfSelectedAccount}"/> 

This will allow you to assign the user-selected account to an instance of the wrapper object that belongs to the external controller. Then you can specify:

 instanceOfSelectedAccount.theAccount 

from the main controller Visualforce Pages.

+4
source

1 - declare a static variable in an external class (may be a VF-page controller)
Sort of:
public static apexType myRecordOutside;
2 -When you make your selection from the entries in the method in the user component controller
Do something like this:
OutsideClass.myRecordOutside = chosenRecord; //notice that when its static you can access it without instantiating the outside class.
3- then declare in your visual power
<c:myCustomComponent userSelectedAccount = {!myRecordOutside}></c:myCustomComponent>
this will get myRecordOutside not from the component controller class, but from the outer class

If you have any questions regarding part of my answer, let me know :)

+1
source
  /* This is an example of getting non static variable value from visualforce component controller variable to visualforce page controller variable */ VF page: DisplayCountryPage <apex:page> <apex:commandButton value="display country list" action="{!displaycountryname}" /> <apex:repeat value="{!displaycountrylistvalue}" var="item"> <div> {!item} </div> </apex:repeat> <c:testvfcmp vfpageclasscontroller="{!thisPageInstance}"/> </apex:page> ===================== DisplayCountryPage VF Page controller: vfpageclass public class vfpageclass{ public List<String> displaycountrylistvalue{get;set;} public vfcomponentclass vfcmpobj{get;set;} public void methodtosetvfcomponentclass(vfcomponentclass vfcmpobj2){ vfcmpobj = vfcmpobj2; } public void displaycountryname(){ displaycountrylistvalue = new List<String>(); displaycountrylistvalue = vfcmpobj.listOfCountry; } public vfpageclass thisPageInstance{ get{ return this; } set; } } ====================== vf component: testvfcmp create an attribute like below: <apex:component controller="CSTSearchPanelController"> <apex:attribute name="vfpageclasscontroller" type="vfpageclass" assignTo="{!vfpageobj}" description="The controller for the page." /> <apex:commandButton value="set country list" action="{!setCountrylist}" /> </apex:component> ===================== <testvfcmp> vf component controller: vfcomponentclass public class vfcomponentclass{ public List<String> listOfCountry = new List<String>(); public vfpageclass vfpageobj{ get; set{ vfpageobj = value; vfpageobj.methodtosetvfcomponentclass(this); } } public void setCountrylist(){ listOfCountry.add('India'); listOfCountry.add('USA'); } } /* This is an example of getting static variable value from visualforce component controller variable to visualforce page controller variable */ VF page: DisplayCountryPage <apex:page> <apex:commandButton value="display country list" action="{!displaycountryname}" /> <apex:repeat value="{!displaycountrylistvalue}" var="item"> <div> {!item} </div> </apex:repeat> <c:testvfcmp vfpageclasscontroller="{!thisPageInstance}"/> </apex:page> ===================== DisplayCountryPage VF Page controller: vfpageclass public class vfpageclass{ public List<String> displaycountrylistvalue{get;set;} public void methodtosetvfcomponentclass(vfcomponentclass vfcmpobj2){ if(vfcmpobj2.getStaticCountryList() !=null){ displaycountrylistvalue = new List<String>(); displaycountrylistvalue = vfcmpobj2.getStaticCountryList(); } /* USE THIS displaycountrylistvalue VARIABLE THROUGHOUT THE CLASS ONCE YOU SET BY HITTING BUTTON <set country list>. DO NOT USE vfcmpobj2.getStaticCountryList() IN OTHER METHODS TO GET THE VALUE, IF DO, IT WILL RETURN NULL*/ } public void displaycountryname(){ } public vfpageclass thisPageInstance{ get{ return this; } set; } } ====================== vf component: testvfcmp create an attribute like below: <apex:component controller="CSTSearchPanelController"> <apex:attribute name="vfpageclasscontroller" type="vfpageclass" assignTo="{!vfpageobj}" description="The controller for the page." /> <apex:commandButton value="set country list" action="{!setCountrylist}" /> </apex:component> ===================== <testvfcmp> vf component controller: vfcomponentclass public class vfcomponentclass{ public static List<String> listOfCountry = new List<String>(); public vfpageclass vfpageobj{ get; set{ vfpageobj = value; vfpageobj.methodtosetvfcomponentclass(this); } } public static void setCountrylist(){ listOfCountry.add('India'); listOfCountry.add('USA'); } public List<String> getStaticCountryList(){ return listOfCountry; } } 
0
source

Source: https://habr.com/ru/post/1339016/


All Articles