1

This is my problem... I have a select list.. When I select the value, according to the value.. the id should be displayed..I done some part remaining part I struck. please find the code below.

   <apex:page controller="selectlist">
  <apex:form >
      <apex:selectList value="{!sv}" size="1" rendered="true">
          <apex:selectOptions value="{!value}">         
          </apex:selectOptions>     
      </apex:selectList>  
  </apex:form>
  <apex:outputPanel id="output">
      <apex:outputLabel value="{!Iddisplay}">
      </apex:outputLabel>
  </apex:outputPanel>

</apex:page>


-------------------------- controller--------------------------
public class selectlist
 {
    public String sv { get; set; }
    public String getIddisplay() 
    {
        return null;
    }


    public List<SelectOption> getValue()
     {
         List<SelectOption> option = new List<SelectOption>();
         option.add(new selectoption('none','---Select--'));
         for(Account ac: [Select Id,name From Account limit 5])
         {
             option.add(new SelectOption(ac.id,ac.name));

         }
         return option;      
    }
}

update: This way I tried and succeed but...

<apex:page controller="SelectList3">
    <apex:form >
        <apex:selectList value="{!slv}" size="1">
            <apex:actionSupport event="onchange" reRender="out" />
            <apex:selectOptions value="{!value}">                     
            </apex:selectOptions>               
        </apex:selectList>
    </apex:form>
    <apex:outputPanel id="out">
        <apex:outputText value="{!slv}">

        </apex:outputText>

    </apex:outputPanel>

</apex:page>

Controller:

public with sharing class SelectList3 
{
    public list<selectoption> getValue()
     {
        list<SelectOption> ls= new list<SelectOption>();
        for(Contact acc:[select id, name from Contact])
        {
            ls.add(new SelectOption(acc.id,acc.name));
        }
        return ls;
    }

    public String slv { get; set; }

}

Now my question is how to pass the select value to a method..and fetch the value of record details with that id...Find detailed explination below..

In above code with the slv will return "id" of the selected value... Now using this Id I want to get related values of Account[ name etc..]

3 Answers 3

2
Use the below code.

public class selectlist
 {
    public String sv { get; set; }

    public List<SelectOption> getValue()
     {
         List<SelectOption> option = new List<SelectOption>();
         map<id,string> smap = new map<id,string>();

         option.add(new selectoption('none','---Select--'));
         for(Account ac: [Select Id,name From Account limit 5])
         {
             option.add(new SelectOption(ac.id,ac.name));
             smap.put(ac.id,ac.name);
         }
         return option;      
    }

    public String getIddisplay() 
    {

      return smap.get(sv);
    }



}
2
  • 1
    Could you add some more information about your solution? Dec 6, 2013 at 7:45
  • 2
    Yeah sure from my understanding of the question he should show name in page accroding to the selected id,so i took one map to store id and related id's name after he selecting the id we should call getIddisplay method for sure in that method i will get the name according to the id as "map.get(id)". and we will display the name in page.
    – Sunny
    Dec 6, 2013 at 8:17
0

After a lot of trials, found the solution....

<apex:page controller="SelectList3">
<apex:form >
<apex:pageBlock >
  <apex:pageBlockSection title="Contact List">
    <apex:selectList value="{!slv}" size="1" label="Contaclist">
        <apex:actionSupport event="onchange" reRender="table" />
        <apex:selectOptions value="{!value}">                     
        </apex:selectOptions>               
    </apex:selectList>
   </apex:pageBlockSection>
 </apex:pageBlock>
</apex:form>
<apex:pageBlock title=" Value of contacts">
<apex:pageBlockSection >
<apex:outputPanel id="out">
   <apex:pageblockTable value="{!tabledata}" var="c" cellpadding="2px" id="table">
       <apex:column value="{!c.name}" headerValue="Account Name"/>


   </apex:pageblockTable>

</apex:outputPanel>
</apex:pageBlockSection>

------------------------ Controller------------------------------------------

public with sharing class SelectList3 
{

public list<Account> getTabledata()
 {           
      list<Account> con=[select name from Account where id=:slv];        
      return con;
 }


   public list<selectoption> getValue()
 {
    list<SelectOption> ls= new list<SelectOption>();
    for(Account acc:[select id, name from Account])
    {
        ls.add(new SelectOption(acc.id,acc.name));
    }
    return ls;
}

public string slv{get;set;}

}

0

Try this i think it's work fine with Action support

<apex:page controller="selectlist">
   <apex:form >
       <apex:selectList value="{!sv}" size="1">
          <apex:selectOptions value="{!value}">         
       </apex:selectOptions> 
             <apex:actionSupport event="onchange" reRender="one"/>
       </apex:selectList>  
   </apex:form>
      <apex:outputPanel id="one">
   <apex:outputLabel value="{!sv}">
  </apex:outputLabel>
  </apex:outputPanel>
  </apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .