You need to sign in to do that
Don't have an account?
![Anu-SFDC Anu-SFDC](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
getting Picklist value in pageblocktable
Hi
This is my visualforce page:
<apex:commandButton value="Save" action="{!Saveacc}"/>
<apex:pageblockTable value="{!fields}" var="fls">
<apex:column value="{!fls.name}" headerValue="Label"/>
<apex:column value="{!fls.key}" headerValue="Key"/>
<apex:column >
<apex:selectList value="{!selectedvalue}" size="1">
<apex:selectOptions value="{!TagField}" id="fval" />
</apex:selectList>
</apex:column>
</apex:pageblockTable>
This is my controller..
public PageReference Saveacc(){
System.debug('selected valueeeeeeeeeeee'+selectedvalue);
Saveaccount();
return null;
}
public SObject Saveaccount(){
System.debug('field valueeeeeeee'+selectedvalue);
List<Mandatory_Object__c> rec= new List<Mandatory_Object__c>();
Set<string> MoNameSet=new Set<string>();
for(Integer i=0; i<fields.size();i++){
MoNameSet.add(fields[i].Name);
}
List<Mandatory_Object__c> insrec = [select Id,Name,Value__c,Type__c,Name__c from Mandatory_Object__c where Id!=null and Type__c =:selectedobject and Name__c = :MoNameSet];
for(Integer i=0; i<fields.size();i++){
Mandatory_Object__c mo = new Mandatory_Object__c();
mo.Name__c=fields[i].name;
// mo.Value__c =fields[i].val;
System.debug('field valueeeeeeee'+selectedvalue);
mo.Value__c = selectedvalue;
mo.Type__c = fields[i].ftype;
rec.add(mo);
}
if(!rec.isEmpty()&&insrec.isEmpty()){
insert rec;
}
else{
update insrec;
}
return null;
}
My show stopper is I have several values in my picklist (selectedvalue).For example I have these 3 values in my picklist.
BillingFirstName
BillingLastName
BillingMI
But when ever i changed the value and trying to save the code i'm always getting first value(BillingFirstName) only.
I have geter and setter methods also for this variables.
How can I get the selected picklist values in selectedvalue attribute?
Anu.
Cool. So just add another string to your pair class of 'selectedvalue', then change the page to use that as the variable for the selectlist in the row.
All Answers
Do you want to display the selected value from the picklist in your controller ?
is your property is of list type..or what?...if yes then you have to write
<apex:selectList value="{!selectedvalue}" multiselect="true">
I think the problem is that you have backed the value for every row of the table with the same variable. I.e. if you have 10 rows in your table, when you submit the page back, each of the 10 picklists will write its selected value in the one selectedvalue property. If the last one to be updated via the postback has the value "BillingFirstName", then they will all have that value.
Is fields a custom class? If so, you could simply add a string to capture the selectedvalue into that. Otherwise I'd suggest you need a wrapper class.
Just If you want to display only selected value in your controller then use this code.
Yes..
Thank you bob..
I checked with my code.. It is taking the last value..
But can you please make it more clarify how to solve this issue ?
Thanks
Anuradha
In your pageblocktable you are iterating a collection of fields - how have you defined these? E.g. are they custom objects, sobjects etc? That's the key to it really.
Hi
This is my fields(Pageblock table iterator) Code.
public void showFields() {
TagField = TagFields();
// fields.clear();
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values()){
schema.describefieldresult dfield = sfield.getDescribe();
Pair field = new Pair();
field.key = dfield.isUpdateable();
field.label = dfield.getLabel();
field.name = dfield.getname();
field.ftype = selectedObject;
if(field.key == true){
fields.add(field);
}
}
}
public class Pair {
public Boolean key {get; set;}
public String val {get; set;}
public String name {get; set;}
public String label{get;set;}
public String ftype{get;set;}
}
Anu
Cool. So just add another string to your pair class of 'selectedvalue', then change the page to use that as the variable for the selectlist in the row.
Hi,
i need some help.
Here is my code snippet.
<apex:page controller="test1" showHeader="false" >
<apex:form >
<apex:pageBlock title="Sample">
<apex:pageBlock title="Range">
<apex:selectList value="{!selectedvalue}" size="1">
<apex:selectOptions value="{!options1}"/>
</apex:selectList>
</apex:pageBlock>
</apex:pageBlock>
</apex:form>
</apex:page>
I have some values in the picklist and once the user selects a value from the list i want to display that value (i.e) label in the same visualforce page in a seperate section.
Regards,
S.Sivakumar