Thursday, January 25, 2018

How to disable a row when Input checkbox is unchecked?

usually in Visualforce if we use Input check box when can re-render the page using ajax calls but the challenge occurs if we use the repeat tags with input field (Checkbox) Component

Here is a simple java script code which will do the job.

<script>  
function confirmDisbaled(ifchecked, id1 ,id2)
    {   
        //alert("Unchecking will reset the values to Zero.");
        document.getElementById(id1).disabled = !ifchecked;    
        document.getElementById(id2).disabled = !ifchecked; 
        document.getElementById(id1).value = 0; 
        document.getElementById(id2).value = 0; 
             
    } 
</script>


and the Visualforce page should be something like this



<apex:commandButton value="Select Products" action="{!editAll}"   rendered="{!isEdit == false}"/>
            <apex:outputPanel rendered="{!isEdit}">
                <apex:commandLink action="{!saveProducts}" styleClass="btn" style="text-decoration:none;padding:4px;" value="Save Products" target="_top"/>
                
                <apex:commandLink action="{!Cancel}" value="Cancel" target="_top" styleClass="btn" style="text-decoration:none;padding:4px;" />
                
                <apex:pageBlockTable value="{!selProdList}" var="opp" id="opp_table" styleclass="slds-table slds-table_bordered slds-table_cell-buffer">
                    <apex:column headerValue="Selected?" styleclass="slds-text-title_caps">
                        <apex:inputField value="{!opp.IsSelected__c}" styleclass="slds-truncate" onchange="return confirmDisbaled(this.checked, '{!$Component.ecl}','{!$Component.emt}');"/>    
                    </apex:column>
                    <apex:column value="{!opp.Product_Name__c}"/>           
                    <apex:column headerValue="Credit Limit (M)" styleclass="slds-text-title_caps">
                        <apex:inputField value="{!opp.Expected_Credit_Limit__c}" styleclass="slds-truncate" id="ecl"  onkeydown="limitfieldvalue('{!$Component.ecl}',13);" onkeyup="limitfieldvalue('{!$Component.ecl}',13);"/>
                    </apex:column>
                    <apex:column headerValue="Max Tenure (Mn)" styleclass="slds-text-title_caps">
                        <apex:inputField value="{!opp.Expected_Max_Tenure__c}" id="emt" onkeydown="limitfieldvalue2('{!$Component.emt}',3);" onkeyup="limitfieldvalue2('{!$Component.emt}',3);"/>
                    </apex:column>
                </apex:pageBlockTable> 
            </apex:outputPanel>


The confirmedisabled() method is set with Inputs in the Check box field component and Id of the repective field which should be disable on clicking checkbox is passed as the input along with IfChecked Value. Changing !(Not) in the java script method will render the inverted outputs.

No comments: