0

I have a picklist and I want a specific value to render two date fields and another picklist field unable to be edited, or "grayed out". How do I accomplish this?

EDIT

            <apex:inputField value="{!contractItem.Contract_Type__c}" onchange="changeVar()" tabOrderHint="40"/>
            <apex:inputField value="{!contractItem.Contract_Start_Date__c}" tabOrderHint="41"/>
            <apex:inputField value="**{!contractItem.Service_Frequency__c}**" tabOrderHint="47"/>
            <apex:inputField value="**{!contractItem.Billing_Frequency__c}**" tabOrderHint="47"/>
            <apex:inputField value="{!contractItem.Service_Date_Time__c}" tabOrderHint="42"/>
            <apex:inputField value="{!contractItem.Contract_Term_months__c}" tabOrderHint="48"/>
            <apex:inputField value="{!contractItem.Requested_First_Service__c}" tabOrderHint="43"/>

The bolded are disabled. Here is the controller

    public void changeVar(){
    if(contractItem.Contract_Type__c == 'Wood Boring Insect'){
        contractItem.Contract_Term_months__c = 0;
    }
}
1

You could bind the disabled attribute of the field to an expression and change it to true onchange of the picklist.

e.g. (VF page)

<apex:outputPanel id='panel'>
    <apex:actionFunction name='changeVar' action='{!changeVar}' rerender='panel'/>
    <apex:selectList value='{!valueVar}' onchange='changeVar()'>
        <apex:selectOptions value='{!items}'/>
    <apex:selectList>
    <apex:inputField value='{!inputVal}' html-disabled='{!disabledVar}'/>
</apex:outputPanel>

Controller code:

public void changeVar(){
    if(valueVar == 'SOME VALUE'){
        disabledVar = true;
    }
}

Another option is using some JS to do it:

<apex:selectList value='{!valueVar}' onchange='changeVar(event)'>
        <apex:selectOptions value='{!items}'/>
<apex:selectList>
<apex:inputField value='{!inputVal}' id='inputToDisable'/>

<script>
    function changeVar(e){
        if(e.target.value == 'SOME VALUE'){
            document.querySelectorAll(input[id$=inputToDisable]")[0].disabled = true;
        }
    }
</script>

Hope this helps!

9
  • In the controller code, is that where I might want a condition for if the picklist field is <field mentioned>? Dec 14 '16 at 14:51
  • @NatalieSpatharakis Correct, I edited my answer to demonstrate Dec 14 '16 at 14:54
  • <apex:inputField value="{!contractItem.Contract_Term_months__c}" disabled="{!disabledVar}" tabOrderHint="48"/> I get "Unsupported attribute disabled in <apex:inputField>" I have been messing around with it for a bit and didn't get this before, but I can't see what I've done wrong.... thoughts? Dec 14 '16 at 15:25
  • @NatalieSpatharakis my mistake, you have to use the disabled attribute this way html-disabled in an apex:inputField. I've updated my answer to reflect that. Dec 14 '16 at 15:34
  • It's not a great solution so far. The logic seems fine, but it's shifting things over when I try to add those components and just adding onChange to one of the fields deactivates two fields, even though I took away the functionality to disable anything at all. Any idea why that would happen? I don't have any conditions to disable anything, I just wanted a value to make the value of another field 0. But when I try it, two picklists are totally disabled. I'll edit question with cod.e Dec 14 '16 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.