+ Start a Discussion
Girish Goswami 16Girish Goswami 16 

In need to display records on visualforce page according to picklist value selected in the dropdown using custom controller.

suppose we choose 10 from picklist the the table should dispaly 10 records and if we choose 20 then the table updates to display 20 records.
Best Answer chosen by Girish Goswami 16
DeveloperSudDeveloperSud
Hi ,

I think you are trying to achieve something like this.Try the below code.
Let us know if this helps and do not forget to mark this as solved if this answers your query .Thanks.
 
<apex:page controller="sample3x" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
 <apex:selectList value="{!numberLimit}" size="1" >
  <apex:selectOptions value="{!records}" />
  <apex:actionSupport action="{!showRecords}" event="onchange" reRender="pbt"/>
 </apex:selectList>
 </apex:pageBlockSectionItem>
 </apex:pageBlockSection> 
 <apex:pageBlockTable value="{!acts}" var="a" id="pbt">
  <apex:column value="{!a.name}" />
  <apex:column value="{!a.id}"/>
 </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
 
public with sharing class sample3x {

   public integer numberLimit{get;set;}
   public list<selectOption> records{get;set;}
   public list<Account> actss{get;set;}
   
   public sample3x(){
   
   actss=new List<Account>();
   records=new List<selectOption>();
   records.add(new selectoption('10','10'));
   records.add(new selectoption('20','20'));
   numberLimit=10;

   }
    
   public List<Account> getActs(){
   actss=[select name,id,industry from account limit :numberLimit];
   return actss;
   }  
    
   public pageReference showRecords(){
   return null;
   }
    
}



 

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Girish,

Please refer an example using a custom drop down list.
VF PAGE:

<apex:page standardController="Account" extensions="OpportunityFilter">
    <apex:form >
        <apex:selectList value="{!regionValue}" size="1">
           <apex:selectOptions value="{!regionOptions}"/> 
        </apex:selectList>
        <apex:commandButton value="Update Opportunities" action="{!updateFilteredOpportunities}"/>
    </apex:form>
    
    <apex:pageBlock >
        <apex:pageBlockTable value="{!filteredOpportunities}" var="opportunity">
            <apex:column value="{!opportunity.name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

CONTROLLER:
public with sharing class OpportunityFilter {
    public String regionValue {get;set;}    
    public List<SelectOption> regionOptions {get;set;}
    public List<Opportunity> filteredOpportunities {get;set;}
    
    public OpportunityFilter(ApexPages.StandardController controller) {
        regionOptions = new List<SelectOption>();
        regionOptions.add(new SelectOption('Manufacturing','Manufacturing'));
        regionOptions.add(new SelectOption('Medical','Medical'));
    }
    
    public void updateFilteredOpportunities() {
        filteredOpportunities = [SELECT id,name FROM Opportunity WHERE Account.Industry = :regionValue];
    }

}

I hope it will be helpful.
  • Please mark it as best answer if the information is informative.

Best Regards
Rahul Kumar
DeveloperSudDeveloperSud
Hi ,

I think you are trying to achieve something like this.Try the below code.
Let us know if this helps and do not forget to mark this as solved if this answers your query .Thanks.
 
<apex:page controller="sample3x" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
 <apex:selectList value="{!numberLimit}" size="1" >
  <apex:selectOptions value="{!records}" />
  <apex:actionSupport action="{!showRecords}" event="onchange" reRender="pbt"/>
 </apex:selectList>
 </apex:pageBlockSectionItem>
 </apex:pageBlockSection> 
 <apex:pageBlockTable value="{!acts}" var="a" id="pbt">
  <apex:column value="{!a.name}" />
  <apex:column value="{!a.id}"/>
 </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
 
public with sharing class sample3x {

   public integer numberLimit{get;set;}
   public list<selectOption> records{get;set;}
   public list<Account> actss{get;set;}
   
   public sample3x(){
   
   actss=new List<Account>();
   records=new List<selectOption>();
   records.add(new selectoption('10','10'));
   records.add(new selectoption('20','20'));
   numberLimit=10;

   }
    
   public List<Account> getActs(){
   actss=[select name,id,industry from account limit :numberLimit];
   return actss;
   }  
    
   public pageReference showRecords(){
   return null;
   }
    
}



 
This was selected as the best answer
Banwari kevat1Banwari kevat1
Hi,
   I think your requiremnet is most probably like following.Please use following controller and vf page and notify me:

VF Page:
<apex:page controller="AllAccountsExtention" >
    <apex:pageBlock>
        <apex:form>
      <apex:pageBlockSection>
          Chose The number of records to dispaly:
        <apex:selectList value="{!noOfRecordsToDisplay}" size="1" dir="LTR">
            <apex:actionSupport event="onchange" reRender="accList" />
            <apex:selectOption itemLabel="5" itemValue="5" />
            <apex:selectOption itemLabel="10" itemValue="10" />
            <apex:selectOption itemLabel="15" itemValue="15" />
            <apex:selectOption itemLabel="20" itemValue="20" />
            <apex:selectOption itemLabel="25" itemValue="25" />
        </apex:selectList>
      </apex:pageBlockSection>
       <apex:pageBlockSection id="accList">
            <apex:dataList value="{!Accounts}" var="account">
                <apex:outputText value="{!account.name}" />
            </apex:dataList>
       </apex:pageBlockSection>
        </apex:form>
    </apex:pageBlock>
</apex:page>

Controller:
public class AllAccountsExtention {
    public Integer noOfRecordsToDisplay {set;get;}
    public AllAccountsExtention()
    {
        noOfRecordsToDisplay = 1;
    }
    public List<Account> getAccounts()
    {   
        System.debug('noOfRecordsToDisplay: '+noOfRecordsToDisplay);
        String query = 'SELECT Name FROM Account LIMIT '+noOfRecordsToDisplay;
        return Database.query(query);
        
    }
}

Thanks
Banwari
Girish Goswami 16Girish Goswami 16
Hi Banwari kevat,

Thanks for the reply it really helped me , but there is one more problem,  when I am using this drop drop down functionality it's working fine but I have implemented the pagination concept also i.e., on clicking next arrow or previous arrow table is not getting updated. only on choosing the picklist the table gets updated but pagination not working. and in the same page i also need to use scrollbar which should also work according to the pagination.   Hope you understand....Thanks in advance
Girish Goswami 16Girish Goswami 16
Hi DeveloperSud,

Thanks for the reply. Please find the above mentioned question also....
Thanks in advance
Tyler McCartyTyler McCarty
Im having the same problem, bud