简体   繁体   中英

How to get in a Visualforce page controller a value from a custom component controller?

I'm trying do develop a visualforce custom component which is an entity chooser. This custom component displays a UI which helps browsing some records. It's possible to select one record, and I'd like to get it from outside the component or its controller.

I've looked at the standard salesforce binding with assignTo bug it's not bidirectional...

Hope someone can help me.. Thanks

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

If you were to pass in a shell object, ie. if your UI is allowing a 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;
  }
}

Page Using the Component:

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

This would allow you to assign the user selected account into the instance of wrapper object which is owned by the outer controller. You can then reference:

instanceOfSelectedAccount.theAccount

from your main Visualforce Pages controller.

1 - Declare a static variable in the outside class (can be the VF page controller)
Something like:
public static apexType myRecordOutside;
2 -When you Make your choice from records in the method within the custom 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 force
<c:myCustomComponent userSelectedAccount = {:myRecordOutside}></c:myCustomComponent>
this will get myRecordOutside not from the component's controller class, but from the outside class

If you have any question about a part of my answer let me know:)

    /* 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;
    }

}                   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM