5

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

我已经查看了使用 assignTo 的标准 salesforce 绑定错误,它不是双向的......

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

4

3 回答 3

5

您是否将对象传递给组件?对象是通过引用传递的,所以如果你的组件有一个属性接受一个对象并对它做一些事情,你的外部页面控制器将能够访问更改的值。

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

于 2011-05-23T23:47:59.360 回答
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

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

于 2012-10-23T11:14:30.850 回答
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;
    }

}                   
于 2019-08-13T11:59:19.963 回答