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

发布于 2024-11-09 02:54:56 字数 174 浏览 0 评论 0原文

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

我查看了标准 salesforce 与 allocateTo 的绑定 bug,它不是双向的...

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦欢 2024-11-16 02:54:56

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

如果你要传入一个 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 控制器引用:

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.

笔落惊风雨 2024-11-16 02:54:56

1 - 在外部类中声明一个静态变量(可以是VF页面控制器)
类似于:
public static apexType myRecordOutside;
2 - 当您从自定义组件控制器中的方法中的记录中进行选择时
做这样的事情:
OutsideClass.myRecordOutside = selectedRecord; //注意,当它是静态的时,您可以访问它而无需实例化外部类。
3-然后,在你的视觉力量中声明
;

这将不是从组件的控制器类获取 myRecordOutside,而是从外部类获取 myRecordOutside

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

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 :)

↘紸啶 2024-11-16 02:54:56
    /* 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;
    }

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

}                   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文