You need to sign in to do that
Don't have an account?
rajs
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: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>
</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'));
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;
}
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.
as you have here all five picklists are bound to one setter.
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.
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
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.
void datamodel( list data to pass ) {
store the data to pass into a picklist
}
Hi.. Ross.
How to write a test method for two classes... (Datamodel class and extension class)
Give solution for it...
Hi All
My Sample code for dynamic field access.