Tag Archives: Visualforce Page

Populate Picklist from Custom Object to the Visualforce Page

Controller:

public with sharing class Sample
{
    public string selectedValue {get; set;}
    public List<SelectOption> industry {get; set;}
    
    public void getIndustry()
    {
        Schema.DescribeFieldResult industryDescription = Account.Industry.getDescribe();
        industry = new List<SelectOption>();
        
        for (Schema.Picklistentry pl : industryDescription.getPicklistValues())
        {
            industry.add(new SelectOption(pl.getValue(),pl.getLabel()));
        }
    }
    
    public void checkValue()
    {
        System.debug('Selected Industry Type - ' + selectedValue);
    }
}

Visualforce Page:

<apex:page controller="Sample" action="{!getIndustry}" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock>
            <apex:pageBlockSection columns="1" >
                <apex:outputLabel value="Industry Type" />
                <apex:selectList size="1" value="{!SelectedValue}" >
                    <apex:selectOptions value="{!industry}"/>
                    <apex:actionSupport event="onchange" action="{!checkValue}" />
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>

Output:

Select All Checkbox Using Javascript in Visualforce Page

Controller:

public with sharing class Sample { 

    public List<AccountWrapper> accountWrapperList {get; set;}
    
    public Sample (){
        if(accountWrapperList == null) {
            accountWrapperList = new List<AccountWrapper>();
            for(Account a: [SELECT Id, Name From Account Limit 10]) {
                accountWrapperList.add(new AccountWrapper(a));
            }
        }
    }
     
    public class AccountWrapper {
        public Account acc {get; set;}
        public Boolean isSelected{get; set;}
 
        public AccountWrapper(Account a) {
            acc = a;
            isSelected = false;
        }
    }  
    
}

Visualforce Page:

<apex:page controller="Sample" sidebar="false" showHeader="false">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,InputID){
            var inputCheckBox = document.getElementsByTagName("input");    
            for(var i=0; i<inputCheckBox.length; i++){          
                if(inputCheckBox[i].id.indexOf(InputID)!=-1){                                     
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!accountWrapperList}" var="a" id="table" title="All Accounts">
                <apex:column >
                    <apex:facet name="header">
                        <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                    </apex:facet>
                    <apex:inputCheckbox value="{!a.isSelected}" id="inputId"/>
                </apex:column>
                <apex:column value="{!a.acc.Name}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output:

Difference between rendered, renderAs and reRender in Visualforce Page

Rendered : It’s a Boolean value and the default value is always true, It works like “display” property of CSS. It is used to place condition for a component(field, outputpanel, section etc), that will show or not on page. (If it is true, it displays the block else it will be hidden).

For Example:
Visualforce Page:
In the controller we need to have get method to assign the value for this variable.

<apex:inputField value="{obj.Filed__c}" Rendered="{!val == true}"/>

Controller:

public boolean val {get;set;}

method(){
val = true;
}

Rerender: After Ajax which component should be refreshed. For this we need to assign id to field, sections or a block. It’s available on commandlink, commandbutton, actionSupport etc.

For Example:
Visualforce Page:

<apex:actionRegion>
    <apex:inputField value="{!TestValue}" >   
        <apex:actionSupport event="onchange" rerender="Id1,Id2,Id3,Id4" action="{!TestMethod}" >
            <apex:param name="Para" value="{!rowNum}" assignTo="{!IndexValue}" />
        </apex:actionSupport>
    </apex:inputField>   
</apex:actionRegion>

Here in actionSupport rerender attribute Id1,Id2,Id3,Id4 are the id’s of field and sections.

RenderAs: It is used for visualforce page show as pdf, excel or any other standard document format.

For Example:
Visualforce Page:

<apex:page controller="TestController" rederAs="pdf">

Difference between action function and action support

Action Function: Invoke the controller method from java script using AJAX and we can use action function from different places on visual force page.

Action Support: Invoke the controller method using AJAX when event occurs on page like onMouseOver, onClick, etc. and we can use action support for particular single apex component.