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
roshmy salesforceroshmy salesforce 

commandButton

Hi All,
I am new in salesforce world and trying to learn as much as I can. While playing around with VF page and Apex class controller I am stuck at one place. I am sharing the details here with code and scenario what I want to achieve. Hope I will get some help here -

Scenario : I want to create a button on VF page and once I click the button it should show me the records on my custom object in blocktable format.
I am able to do and see result thru SOQL and also when I am trying to display the data directly on the VF page ( without button) it showing fine. But I want when I click then only it should show the data n VF page.
Custom object Name : Employee__c

Apex Class code :-
public class EmployeeClass
{

    public PageReference myemps() {
        return null;
    }

    List <Employee__c> emp = [select Name,Salary__c,Skillset__c,Employee_ID__c from Employee__c ] ;
    
    public  List <Employee__c> getmyemps()
    {
        return (emp);
    }
     
}

VF Page code : 

<apex:page controller="EmployeeClass">
        <apex:form >
            <apex:pageBlock >
                  <apex:commandButton value="ListButton" action="{!myemps}" />
                      
              <apex:pageBlockSection >
                      <apex:pageBlockTable value="{!myemps}" var="i">
                                <apex:column value="{!i.Name}"/>
                                  <apex:column value="{!i.Salary__c}"/>
                                <apex:column value="{!i.Skillset__c}"/>
                                    <apex:column value="{!i.Employee_ID__c}"/>
                    </apex:pageBlockTable>  
                
              </apex:pageBlockSection> 
             </apex:pageBlock>
        </apex:form>
</apex:page>
Current code output
 
Sachin AiyappaSachin Aiyappa
Hi Roshmy,
              Use reRender attribute in apex command button and rendered attribute in pageblock Table 

Below is the sample code for reference :
Controller:
public class EmployeeClass{
    public string EnteredText{get;set;}
    public List<Employee__c > emp {get;set;}
    public Boolean showSection1{get;set;}
    
    public PageReference myemps()
    {
          emp = [select Name,Salary__c,Skillset__c,Employee_ID__c from Employee__c WHERE Name Like :'%'+EnteredText+'%' ];];
        showSection1= true;
        return null;        
    }
}

Visualforce page :
<apex:page controller="EmployeeClass">
	<apex:form >
		<apex:outputLabel style="font-weight:bold;" value="Enter Name to Search: " >
        </apex:outputLabel>
        <apex:inputText value="{!EnteredText}"/>
       	<apex:commandButton value="Search" action="{!SearchResult}" reRender="block"/>
        <apex:pageBlock id="block">
        	  <apex:pageBlockTable value="{!myemps}" var="I" rendered="{!showSection1}">
                    <apex:column >
                        <apex:facet name="header">Name</apex:facet>{!I.Name}
                    </apex:column>
                  	<apex:column >
                        <apex:facet name="header">Salary</apex:facet>{!I.Salary__c}
                  	</apex:column>
                  	<apex:column >
                        <apex:facet name="header">Skillset</apex:facet>{!I.Skilllset__c}
                  	</apex:column>
                    <apex:column >
                        <apex:facet name="header">Employee ID</apex:facet>{!I.Employee_ID__c}
                    </apex:column>
              </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>    
</apex:page>

Hope this will meet your requirement
If this helps you, Please Mark this answer as Best Answer.

Regards,
Sachin.
Tridib ChattopadhyayTridib Chattopadhyay
I think the SearchResult method is missing in the class and the pageblock table value should be the object instance name not the method.Other than that it looks good.