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

时间:2011-05-23 21:37:53

标签: salesforce apex-code visualforce

我正在尝试开发一个visualforce自定义组件,它是一个实体选择器。此自定义组件显示一个帮助浏览某些记录的UI。可以选择一条记录,我想从组件或其控制器外部获取它。

我看了标准的salesforce绑定与assignTo错误它不是双向的......

希望有人可以帮助我.. 感谢

3 个答案:

答案 0 :(得分:4)

您是否将对象传递给组件?对象通过引用传递,因此如果组件具有接受对象并对其执行某些操作的属性,则外部页面控制器将能够访问更改的值。

如果你要传入一个shell对象,即。如果您的UI允许用户选择帐户。

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 Pages主控制器。

答案 1 :(得分:1)

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不是来自组件的控制器类,而是来自外部类

如果您对我的部分答案有任何疑问,请告诉我:)

答案 2 :(得分:0)

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

}