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
RAJU_DEEPRAJU_DEEP 

Dynamic drop down list.

Hello,

           I am creating a dropdown list which has to contain the resultant data from the query. Suppose i fire the query [select id, name from Items__c] after that i want to put the retrieved records of the name field in the drop down list. Is there any way to figure out this problem. Any suggestion regarding this will be helpfull for me...

Thanks.

Anand@SAASAnand@SAAS

Please refer to documentation for <apex:selectList> here. That has an example of to go about do this. The example uses a hardcoded list of values but you can iterate over a SOQL to populate the same.

imuino2imuino2
<!-- Page: -->
<apex:page controller="sampleCon">
<apex:form>
<apex:selectList value="{!countries}" multiselect="true">
<apex:selectOptions value="{!items}"/>
</apex:selectList><p/>

<apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
</apex:form>

<apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
<apex:facet name="stop">
<apex:outputPanel>
<p>You have selected:</p>
<apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
</apex:outputPanel>
</apex:facet>
</apex:actionstatus>
</apex:outputPanel>
</apex:page>

/*** Controller: ***/
public class sampleCon {
String[] countries = new String[]{};

public PageReference test() {
return null;
}

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('US','US'));
options.add(new SelectOption('CANADA','Canada'));
options.add(new SelectOption('MEXICO','Mexico'));
return options;
}

public String[] getCountries() {
return countries;
}

public void setCountries(String[] countries) {
this.countries = countries;
}
}

 

Thats the example from the salesforce documentation you can access it by going to your org, then edit the url and after .com put apexpages/apexcomponents.apexp

 

Ignacio.

RAJU_DEEPRAJU_DEEP

This thing i already tried but i want insteadof this data, the resultant data from query should be fixed in the selectOption which should be shown in the form of drop down list...

Is there any way to fulfill this task....

imuino2imuino2

Follow the steps of this example, but instead of filling the options as it does do something like this

 

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
            List<Country__c> countries = [Select Name From Country__c];
            for(User c : countries){
               options.add(new SelectOption(c.Name, c.Name));
            }
            return options;
}
<!-- Page: -->
<apex:page controller="sampleCon">
<apex:form>
<apex:selectList value="{!countries}" multiselect="true">
<apex:selectOptions value="{!items}"/>
</apex:selectList><p/>

<apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
</apex:form>

<apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
<apex:facet name="stop">
<apex:outputPanel>
<p>You have selected:</p>
<apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
</apex:outputPanel>
</apex:facet>
</apex:actionstatus>
</apex:outputPanel>
</apex:page>

/*** Controller: ***/
public class sampleCon {
String[] countries = new String[]{};

public PageReference test() {
return null;
}

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('US','US'));
options.add(new SelectOption('CANADA','Canada'));
options.add(new SelectOption('MEXICO','Mexico'));
return options;
}

public String[] getCountries() {
return countries;
}

public void setCountries(String[] countries) {
this.countries = countries;
}
}