function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TuxTux 

Passing a value to a controller of a custom component, from apex:attribute

I wish to access a List ("alist" in the below attribute) that is passed in a custom component. I am having trouble accessing it. Here is my custom component:

<apex:component controller="testController" >
    <apex:attribute name="title" description="This is the title of the component" type="String" required="true"/>
    <apex:attribute name="count" description="This is the count of the component" type="String" required="true"/>
    <apex:attribute name="color" description="Color to set as background" type="String" required="true"/>
    <apex:attribute name="alist" description="A list of values" type="List" required="false"/>
</apex:component>

 

Here is my controller:

public with sharing class testController {

    public String title {get; set;}
    public List<String> alist {get; set;}
    public List<cValue> customWrap {get; set;}
    
    public testController() {
        System.debug('Title of ::'+title);
        System.debug('Entering custom controller.');
        customWrap = new List<cValue>();
        System.debug('alist in testcontroller::'+alist);
    }
    
    public class cValue {
        public String con {get; set;}
        public Integer value {get; set;}
    
        public cValue(String country, Integer val) {
            con = country;
            value = val;
        }
    }
}

And this is how I call my custom component in the VF page:

 

<c:TestSubPageBlock title="Test" count="3" color="#342145" alist="{!ListOfValues}"></c:TestSubPageBlock>

 

MattLacey.ax1065MattLacey.ax1065

If memory serves correctly, you just need to specify the assignTo attribute in your attribute:

 

<apex:attribute name="alist" description="A list of values" type="List" required="false" assignTo="{!alist}"/>

 

 

TuxTux

Hi Matt, thanks for replying.

 

I did as you said, and I can access the value, but only on the component page. I cannot access the value in the controller. Debugging tells me that the value is null.

 

<apex:component>
<apex:attribute name="alist" description="a list" type="Object" required="false" assignTo="{!alisttest}"/>
<div id = "{!subid}"> <apex:repeat value="{!alisttest}" var="cnt"> {!cnt}<br/> </apex:repeat> </div>
</apex:component> 

My controller:

 

public with sharing class testController {

    public String title {get; set;}
    public Map<String, Integer> amap {get; set;}
    public List<String> alisttest {get; set;}
    
    public List<cValue> customWrap {get; set;} 
    
       public void setalisttest (List<String> al) {
        alisttest = al;
        System.debug('alisttest in testcontroller in constructor::'+alisttest);
//getting null here. } public List<String> getalisttest() { return alisttest; } }
TuxTux

ADDENDUM:

 

I can access the value of alisttest in the repeat tag. But, as I said, its not accessible in the controller.

hemantgarghemantgarg

Hi,

 

Component attribute values are not accessible in constructor of the component controller, However there are other methods to access them :-

1) you can call an action function on load of the component , in that method( action in controller) it will be accesible.

2) in any of the getter method it will be accessible like following :-

 

sample code.

 

public class controller {

      //-------------------------------------//

public string getMyParam(){

             System.debug('========your atribute===========' + alist);

             return 'yes';

         }

}

 

<apex:component controller="controller">

     <apex:attribute name="alist" type="list" ................ />

      {!myparam}

<apex:component>

MattLacey.ax1065MattLacey.ax1065

Apologies for that , as hemantgarg as stated, you can't access the values in the contstructor. This has always been a bit of a pain point for me with components.

 

I tend to have a private boolean memeber called initialised, then check that in every getter / setter. If it's false I run an Initialisaton method and set it to true.

Chamil MadusankaChamil Madusanka

Try this

 

<apex:component controller="testController" >
    <apex:attribute name="title" description="This is the title of the component" type="String" required="true"/>
    <apex:attribute name="count" description="This is the count of the component" type="String" required="true"/>
    <apex:attribute name="color" description="Color to set as background" type="String" required="true"/>
    <apex:attribute name="alist" description="A list of values" type="List" assignTo="{!alist}" required="false"/>
</apex:component>

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

hemantgarghemantgarg

Hi,

 

Please note that what I have said applied for the attribute values, so what I want to say is , the attribute vales which are passed to the component are not accessed inside the contructor(the value that is being passed to component). I have experienced the same issue.

abivenkatabivenkat

 

hi,

 

 

I have created a custom Component that provides a custom dynamic lookup feature. The component and its controller reads a saved query and retrieves the records for the query and show them in a pop-up dialog box. I get the value and Id of the record selected and am able to show it in the input text within the component. To save the Textbox value, i have to get the value from the components input text and pass it to the visualforce page's controller. so i require help in passing the value and Id of the selected record from the component to the Visualforce page where it is included. Please help or suggest any ideas to achieve this functionality! 

Thanks,
abivenkat,
SFDC Learner
hemantgarghemantgarg

As I think component cannot return any value to its caller, but at the run time the compoenent html code is copied to the caller vf page same as any other vf tag. So you can inspect the textbox id(compoennt's textbox) from the generated html, and get its value by using "document.getElementById(<Textbox Id>).value" .

 

abivenkatabivenkat

 

hi hemantgarg,

 

if i put this line of code in my custom component, will it retrieve the value from Visualforce page's Textbox??? 

will it search the Visualforce page for this id first???

 

 

 

thanks,

abivenkat,

SFDC Learner

hemantgarghemantgarg

Please give it try, I hope it should work, because on generated html the ids of every html tag is unique.

Mitesh SuraMitesh Sura
MattLacey,

Can you please post same code for

"I tend to have a private boolean memeber called initialised, then check that in every getter / setter. If it's false I run an Initialisaton method and set it to true."

regards
Mitesh