You need to sign in to do that
Don't have an account?
sandeshd
Controlling picklist fields in a vf page
Hi,
I have two picklist fields and the value of one should depend on the other. I cannot use dependent picklists as I get the values into the picklist through some logic in controller class.
I am using selectOptions for dispalying the picklist values. I tried using actionSuppport , onchange functionality to change the values but wasnt successful.
Please let me know how can I achieve this.
Thanks.
Can you post the code?
<apex:pageblocksectionitem id="pageItemMod">
<apex:outputtext >Mods</apex:outputtext>
<apex:selectList id="mod2" size="1" value="{!modSelected}" multiselect="false">
<apex:selectOptions value="{!mods}"/>
<apex:actionSupport status="status" event="onchange" reRender="mdesc" action="{!getDescriptionList}"/>
</apex:selectList>
</apex:pageblocksectionitem>
<apex:pageblocksectionitem id="mdesc" >
<apex:outputtext >Description</apex:outputtext>
<apex:selectList size="1" value="{!Description}" multiselect="false">
<apex:selectOptions id="mdescription" value="{!mountDescList}"/>
</apex:selectList>
</apex:pageblocksectionitem>
controller:
------------------
public PageReference getDescriptionList()
{
Schema.Describefieldresult result = Schema.sObjectType.Quote.fields.Description__c;
result = result.getSObjectField().getDescribe();
System.debug(Logginglevel.error+'result.getPicklistValues() #### ' + result.getPicklistValues());
for(Schema.picklistentry f : result.getPicklistValues())
{
if(f.getValue() == 'SM' && (modSelected == 'abc' || Quote.System_Model__c == 'abc'))
continue;
mountDescList.add(new SelectOption(f.getValue(),f.getValue()) );
}
return null;
}
// My First Question why you are returning PageReference here.
//You did not put any filter logic for your dependent picklist
public getDescriptionList()
{
// Remove the below commented code because it's supplying same picklist values to your VF Page.
/* Schema.Describefieldresult result = Schema.sObjectType.Quote.fields.Description__c;
result = result.getSObjectField().getDescribe();
System.debug(Logginglevel.error+'result.getPicklistValues() #### ' + result.getPicklistValues());
for(Schema.picklistentry f : result.getPicklistValues())
{
if(f.getValue() == 'SM' && (modSelected == 'abc' || Quote.System_Model__c == 'abc'))
continue;
mountDescList.add(new SelectOption(f.getValue(),f.getValue()) );
} */
// Write you filteration critaria code here, add result in to mountDescList list.
// and you should put your code to getter metod of dependent piclist
}
public void getDescriptionList()
{
Schema.Describefieldresult result = Schema.sObjectType.Quote.fields.Description__c;
result = result.getSObjectField().getDescribe();
System.debug(Logginglevel.error+'result.getPicklistValues() #### ' + result.getPicklistValues());
for(Schema.picklistentry f : result.getPicklistValues())
{
if(f.getValue() == 'SM' && (modSelected == 'abc' || Quote.System_Model__c == 'abc'))
break;
mountDescList.add(new SelectOption(f.getValue(),f.getValue()) );
}
// REMOVE return statement
}
There is no point to use PageReference in the above scenario. I guess, You want to show Description picklist values excluding values where modSelected is abc etc.
Let me know if I am pointing you in correct direction.
Cheers!
If I use a break statement there then it comes out of the for loop right which I dont want. I just want it to skip adding it to the that particular list mountDescList only if that condition is satisfied.
Hi Shiv,
ok I dont need toreturn page reference here.
My filter logic is
if(f.getValue() == 'SM' && (modSelected == 'abc' || Quote.System_Model__c == 'abc'))
continue;
mountDescList.add(new SelectOption(f.getValue(),f.getValue()) );
If the above if condition is satisfied then I am not adding it to the list mountdesclist and if it is not satisfied then the value will be added to the list. I just want to omit the value of SM getting added to the list if modselected == 'abc' or if Quote.System_Model__c == 'abc'
Please let me know if you still need any information.
You can write your If logic as follows:
if(!(f.getValue() == 'SM' && (modSelected == 'abc' || Quote.System_Model__c == 'abc'))) {
mountDescList.add(new SelectOption(f.getValue(),f.getValue()));
}
Cheers!
Hi Sandeshd,
modSelected == 'abc' || Quote.System_Model__c == 'abc' // this condition gives sense that if abc is selceted in another picklist and Quote.System_Model__c is haveing value 'abc' (i think Quote is standard controller for your page.)
But i am not able to understood what you are doing in this condition f.getValue() == 'SM'
Might be below code helps you