简体   繁体   English

如何在 Visualforce 页面 controller 中获取自定义组件 controller 的值?

[英]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.我正在尝试开发一个作为实体选择器的visualforce自定义组件。 This custom component displays a UI which helps browsing some records.此自定义组件显示有助于浏览某些记录的 UI。 It's possible to select one record, and I'd like to get it from outside the component or its controller. select 一条记录是可能的,我想从组件外部或其 controller 获取它。

I've looked at the standard salesforce binding with assignTo bug it's not bidirectional...我查看了标准 salesforce 与 assignTo 绑定的错误,它不是双向的......

Hope someone can help me.. Thanks希望有人可以帮助我..谢谢

Are you passing an object into the component?您是否将 object 传递到组件中? 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.对象是通过引用传递的,所以如果你的组件有一个属性,它接受一个 object 并对它做一些事情,你的外部页面 controller 将能够访问更改的值。

If you were to pass in a shell object, ie.如果您要传递 shell object,即。 if your UI is allowing a user to select an Account.如果您的 UI 允许用户使用 select 帐户。

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:组件 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.这将允许您将用户选择的帐户分配到由外部 controller 拥有的包装器 object 的实例中。 You can then reference:然后你可以参考:

instanceOfSelectedAccount.theAccount

from your main Visualforce Pages controller.从您的主要 Visualforce 页面 controller。

1 - Declare a static variable in the outside class (can be the VF page controller) 1 - 在外部 class 中声明一个 static 变量(可以是 VF 页面控制器)
Something like:就像是:
public static apexType myRecordOutside;
2 -When you Make your choice from records in the method within the custom component controller 2 - 当您从自定义组件 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 3-然后,在你的视觉力中声明
<c:myCustomComponent userSelectedAccount = {:myRecordOutside}></c:myCustomComponent>
this will get myRecordOutside not from the component's controller class, but from the outside class这将不是从组件的 controller class 获得 myRecordOutside,而是从外部 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;
    }

}                   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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