如何在Visualforce页面控制器中从自定义组件控制器获取值?

| 我正在尝试开发一个visualforce自定义组件,它是一个实体选择器。此自定义组件显示一个UI,可帮助浏览某些记录。可以选择一个记录,我想从组件或其控制器的外部获取它。 我已经看过标准Salesforce绑定与assignTo的bug,它不是双向的... 希望可以有人帮帮我.. 谢谢     
已邀请:
您是否正在将对象传递到组件中?对象是通过引用传递的,因此,如果您的组件具有接受对象并对其执行操作的属性,则外部页面控制器将能够访问更改的值。 如果要传递一个外壳对象,即。如果您的用户界面允许用户选择一个帐户。
Class SelectedAccount
{
  public Account theAccount {get;set;}
}
零件:
<apex:component controller=\"ComponentController\">
   <apex:attribute type=\"SelectedAccount\" name=\"userSelectedAccount\" description=\"Selected Account\" assignTo=\"{!selectedAccount}\"
</apex:component>
组件控制器:
public class ComponentController
{
  public selectedAccount;

  public void ComponentController(){}

  public PageReference selectAccountFromUI(Account selected)
  {
    selectedAccount.theAccount = selected;

    return null;
  }
}
页面使用组件:
<c:MyAccountComponent userSelectedAccount=\"{!instanceOfSelectedAccount}\"/>
这将允许您将用户选择的帐户分配到外部控制器拥有的包装对象的实例中。然后,您可以参考:
instanceOfSelectedAccount.theAccount
从您的主要Visualforce页面控制器。     
1-在外部类中声明一个静态变量(可以是VF页面控制器)    就像是 :    
public static apexType myRecordOutside;
2-当您从自定义组件控制器内的方法中的记录中进行选择时 做这样的事情:     
OutsideClass.myRecordOutside = chosenRecord; //notice that when its static you can access it without instantiating the outside class.
3-然后,以你的视觉力量宣告
<c:myCustomComponent userSelectedAccount = {!myRecordOutside}></c:myCustomComponent> 
这将不是从组件的控制器类中获取myRecordOutside,而是从外部类中获取myRecordOutside 如果您对我的答案有任何疑问,请告诉我:)     
    /* 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;
    }

}                   
    

要回复问题请先登录注册