You need to sign in to do that
Don't have an account?
Tux
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>
If memory serves correctly, you just need to specify the assignTo attribute in your attribute:
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.
My controller:
ADDENDUM:
I can access the value of alisttest in the repeat tag. But, as I said, its not accessible in the controller.
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>
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.
Try this
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.
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.
hi,
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" .
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
Please give it try, I hope it should work, because on generated html the ids of every html tag is unique.
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