Wednesday, 1 August 2012

Passing values of parameters to customcomponent from visualforce page

I am explaining here how to pass values from visualforce page to custom component.
To pass values from visualforce page we should have <apex:attribute> tag with name,type,assignto and description properties.
Now i am writing custom componet.
To write custom component we need to go setup->Appsetup->Develop->components
Apex components  starts with <apex:component> and ends with </apex:component>
eg:
==
<apex:component controller="controllerforcomponent">
    <apex:attribute name="sobjectname" type="string" assignTo="{!objectname}" required="true" description="for objectname storing"/>
    <apex:pageBlock >
        <apex:pageBlockTable value="{!records}" var="a">
            <apex:column value="{!a.id}"/>   
            <apex:column value="{!a['firstname']}"/>           
        </apex:pageBlockTable>   
    </apex:pageBlock>
</apex:component>
==========================================================================
controller
===========
public  class controllerforcomponent {
     public String objectname{get;set;}
     public LIst<sobject> lstData{get;set;}
     public String sQuery;
     public List<Sobject> getrecords(){
         sQuery ='';
         sQuery = sQuery + 'select id,firstname,lastname from ' + objectname;
         system.debug('squery:'+squery);
         lstdata = new LIst<Sobject>();
         lstdata = database.query(sQuery);
         if(lstData.size() != null && lstdata.size()>0)
             return lstdata;
         else
             return null;
     }
       
}
==========================================================================

=Here"name" property is useful for pass values to component from visualforce page.When we set the  value to "name" property value called sobjectname as shown below.

<apex:page sidebar="false" >
    <c:Mycustomcomponent sobjectname="user"/>
</apex:page>

 this "user" will pass to customcomponent through sobjectname and assignto string property  called " objectname"  through <apex:attribute> tag in  our controller called "controllerforcomponent".
Our business logic is writing in controller.


output for page with customcomponent:
===============================



3 comments:

  1. Can I pass list having date/String as argument for "sobjectname"?
    If yes, please guide me how to do that.

    Thanks in advance

    ReplyDelete