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
rajsrajs 

How to access Values from Dynamicaly added fields

Hi,
    I am new to Visualforce.
I have created five picklist fields by dynamicaly (b'cause of my requirements) by using PageBlockTable.I have used "setter" method to access field values.
but i could  access  only fifth picklist value (last field),insted of accesing all the picklist values.because setter method workes only on last picklist.
please help me:How to get selected values from all the five picklist.
My visualforce code:
 
<apex:page controller="Dynamicsample" id="thepage">
 <apex:pageBlock>   
   <apex:form>
     <apex:pageBlockTable value="{!tabval}" var="tabl">
               <apex:column>
                          <apex:selectList value="{!countries}" multiselect="false" size="1">
                                  <apex:selectOptions value="{!items}"/>
                                 <apex:actionSupport event="onchange" reRender="out" status="status"/>
                          </apex:selectList>
                </apex:column>
       </apex:pageBlockTable> 
       </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:pageBlock>
</apex:page>
 
Controller Code:
 
 
public class Dynamicsample {
 
        String countries;
  Integer[] g=new Integer[]{1,2,3,4,5};
       
        public List<SelectOption> getItems() {
                List<SelectOption> options = new List<SelectOption>();
                options.add(new SelectOption('US','US'));
                options.add(new SelectOption('India','India'));
                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;
          }
  public Integer[] gettabval()
    {
       return g;
    }
 }
 
please debug my code.let me know where i made a mistake.
 
Ron HessRon Hess
you will need to build a list of picklists, and a data model to hold the setter for each of them.

as you have here all five picklists are bound to one setter.
rajsrajs

Hi Ron,

Thanks for ur reply.I understand separate setter needed for each picklists.

is it possible to write a separate  setter for each of the dynamically created picklist?

if it possible,please give me  one example controller code .

Thanks in advance.

 

Ron HessRon Hess
is it possible to write a separate  setter for each of the dynamically created picklist?

Yes, you can write a list of them.  to do this build a loop
 to loop, you need a data model
   each data record would have one picklists and one selected value

   then you loop , creating new datamodel records for each picklist

here is pseudo code

  class datamodel {
     public  List<SelectOption>options { get; set; }
     public String Countries {get; set;}
    // build a constructor for one picklist...
    void datamodel(  passed in data ) {   options = new List<SelectOption>();
                options.add(new SelectOption('US','US'));                 options.add(new SelectOption('India','India'));
                options.add(new SelectOption('CANADA','Canada'));
                options.add(new SelectOption('MEXICO','Mexico'));
                }
  }

  list <datamodel> mylist  {get; set;}
  for ( i < 5 ; i++ ) 
     mylist.add ( new datamodel( pass to constructor ) )



now mylist can be used in a visualforce page, in a repeat, datalist or pageblocktable tag


Message Edited by Ron Hess on 09-15-2008 11:35 AM
rajsrajs
Sorry Ron, Still I have more doubt on Creating separate Setter method and your Data model code.
  • void datamodel()

Here datamodel is a  Constructor without argument, so how can I pass value to,

mylist.add ( new datamodel( pass to constructor ) ).

if you don't mine, can you send me Some more details with Example code.

 

Thank u very much Ron.

Ron HessRon Hess
You can write a constructor to take the data, yes.

void datamodel( list data to pass ) {

store the data to pass into a picklist

}
VisualForceVisualForce

Hi.. Ross.

  How to write a test method for two classes...  (Datamodel class and extension class)

Give solution for it...

rajsrajs
Hi Ron
       Thank you so much,
       Now i have clear idea to handle the Dynamic fields.
       my code is working  fine.
 
Regards,
Raj
 
rajsrajs

Hi All

My Sample code for dynamic field access.

Code:
//TaskDatamodel is a seperate Apex Class
// This class have Set and Get method for Dynamic Picklist
public class TaskDatamodel
{
     public List<SelectOption> options3 = new List<SelectOption>();
     public String COnName;
           

public List<SelectOption> getoptions1()
  {
                Contact[] CName=[Select Id,Name from Contact];
                options3.add(new SelectOption('None','None')); 
                for(Contact  X:CName)
                {

                        options3.add(new SelectOption(X.Name,X.Name));

                }

        return options3;
  }

public String getContactName() {
return COnName;
}
public void getContactName(String t0) {
COnName= t0;
}

// test method here

}


//write a gettabval()  method in your actual Apex class

list <TaskDatamodel> mylist =new list <TaskDatamodel>(); 

public list <TaskDatamodel> gettabval()
{
      
       
         for(Integer j=0;j<5;j++)  //Here 5 object of TaskDatamodel Apex class is created and assigned to a List
          {
             mylist.add ( new TaskDatamodel() ); //each object having  get and set method of picklist
          }
          
 return mylist;
}

// Visual force Page Code

<apex:pageBlockTable value="{!tabval}" var="DMTAB" id="tabpg">  // this table will show 5 Picklist

                 <apex:column > 
                 <apex:facet name="header">Area</apex:facet>
                 <apex:selectList value="{!DMTAB.ContactName}" multiselect="false" size="1" id="pick1" >
                 <apex:selectOptions value="{!DMTAB.options1}"/>
                 </apex:selectList>
                 </apex:column>
                
</apex:pageBlockTable>


 

It will rock