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
Venkat MaddipatiVenkat Maddipati 

How to disable the apex:inputField component value based on rendering criteria?

Hi ,

 

How to disable the apex:inputField  component value based on rendering criteria?

 

 

 

Thanks in advance.

 

 

MagulanDuraipandianMagulanDuraipandian

Sample Code:


Visualforce page:

 

<apex:page controller="sample">

<script type="text/javascript">

</script>
  
    <apex:form >
  
    <apex:pageMessages />
  
    <apex:pageBlock >
        <apex:pageBlockSection columns="2">
            <apex:pageblockSectionItem >
                <apex:outputLabel value="Metro"/>
            </apex:pageblockSectionItem>      
            <apex:pageblockSectionItem >              
                <apex:inputCheckbox value="{!metro}">           
                    <apex:actionSupport event="onchange" reRender="a" action="{!demo}"/>
                </apex:inputCheckbox>
            </apex:pageblockSectionItem>
            <apex:pageblockSectionItem >
                <apex:outputLabel value="City"/>
            </apex:pageblockSectionItem>
            <apex:pageblockSectionItem >
                <apex:inputField value="{!city}" id="a" disabled="{!bool}"/>
            </apex:pageblockSectionItem>          
        </apex:pageBlockSection>      
    </apex:pageBlock>

    </apex:form>

</apex:page>

 

Apex Code:

 

public class sample
{  
    public Boolean metro {get;set;}
    public Boolean city {get;set;}
    public Boolean bool {get;set;}
  
    public sample()
    {
      
    }
  
    public void demo()
    {
        if(metro)
        {
            bool = true;
        }
        else
        {
            bool = false;
        }       
    }
    
}

 

Regards,

Magulan D

Salesforce.com certified Force.com Developer.

SFDC Blog

 

If this post is your solution, kindly mark this as the solution.

 

Venkat MaddipatiVenkat Maddipati

Thanks for ur reply..

 

The apex:inputField component is not supporting the attribute as "disabled".

 

JHayes SDJHayes SD

You can do this via jQuery.  Import the jQuery library as a static resource first -- in my example I named the resource JQuery_1_7_2.  Then you can do this within your page:

 

<apex:page standardController="Account" extensions="TestController">
<apex:includeScript value="{!$Resource.JQuery_1_7_2}"/> <script type="text/javascript"> var j$ = jQuery.noConflict();
var shouldDisable = '{!shouldDisable}'; j$(document).ready(function() {
if (shouldDisable) { j$('input[name$="acctName"]').attr('disabled','disabled');
} }); </script>

.....

<apex:inputField id="acctName" value="{!account.Name}" />

...

You define the property shouldDisable on your controller and can initialize it in your constructor or later in the code:

 

...
    public TestController(ApexPages.StandardController ctlr)  
    {  
       sc = ctlr;
       shouldDisable = true;             
    }
...

      public Boolean shouldDisable 
    { get; set; }

...

 

Regards, jh

JHayes SDJHayes SD

Made some edits to my earlier post -- jh