+ Start a Discussion
Abhishek Singh 88Abhishek Singh 88 

How to displays values in List into picklist on visualforce page

Hi everyone,
I want to create a picklist on a visualforce page , And the values of picklist should be values in a List.
Eg. I have a list called "CCO_producttype". And now on the VF page i want to display Values(CCO_producttype) in a dropdown format.
How would i approach. Any suggestion on this would be highly appriciated.
Prashant Pandey07Prashant Pandey07
Hi Abhishek,
Please check the following code for your reference.. Here I took the list of product and trying to display a picklist on the vf.

Apex Class:
 
public class productlist{

    public productlist(ApexPages.StandardController controller) {

    }
public  list<SelectOption> getpicklistitem(){
    list<SelectOption> options = new list<SelectOption>();
    for(list<Product2> prod :[select id,name from Product2 limit 10]){
            for(Product2 pr:prod)
             options.add(new SelectOption(pr.id, pr.Name));
        }
          return options;
    }
}

Vf
 
<apex:page standardController="product2" extensions="productlist">
    <apex:form id="theForm">
        <apex:pageBlock >
        <apex:pageBlockSection>
        <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Product List" />
                    <apex:selectList value="{!product2.ID}"  size="1">
                        <apex:selectOptions value="{!picklistitem}" />
                    </apex:selectList>
                </apex:pageBlockSectionItem>  
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

--
Thanks,
Prashant

 
Abhishek Singh 88Abhishek Singh 88
Hi Prashant,

Thanks for Acknowledging.
But the list i have is String type. List<string> CCO_producttype. So when I am itrating over for loop in order to add values in select option, it throwing error since list is string and i am using custom settings instance to itrate over it. so stucked here.
Prashant Pandey07Prashant Pandey07
Hi Abhishek,
I was not knowing the above scenario, well please check the below code and let me know if that helps you..
 
public class productlist{

    public productlist(ApexPages.StandardController controller) {

    }

public  list<SelectOption> getpicklistitem(){
// I have state as a custom setting 

List<state__c> sta=[select id,Name from state__c];
List<string> CCO_producttype = New list<String>();
   for(State__c s: sta){
   CCO_producttype.add(String.valueOf(s.name));
 }
 list<SelectOption> options = new list<SelectOption>();
           for(string pr:groups)
            options.add(new SelectOption(pr, pr));
            return options;
    }

}

--
Thanks,
Prashant
Prashant Pandey07Prashant Pandey07
Small correction in Line no 16 remove groups and add CCO_producttype.


--
Thanks,
Prashant