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
AshAsh 

disabled attribute on input checkbox not getting slected if originally disabled then selected

Hi,

 

I have a page where there are 2 checkbox columns (primary and secondary). Only one primary can be selected. I am disabling the secondary checkbox coulmn if that corresponding primary row is selected so that the same primary and secondary cannot be selected. However multiple secondary can be selected.

 

I also have a default value for primary when rendering my VF page, so the corresponding secondary is disabled intially. All this works fine expect that my initially disabled secondary row never retrun a selected value=true  when passed to the colroller although I selected it my UI.

 

In the code below I am defaulting my primary to "Sam" (assuming I have a contact by that name in my app) the corresponing secondary row is disabled. But when I change the primary and choose 3 seconday and one of them is "Sam", then value retured to my controller is only 2 records instead on 3. Initial disabled secondary is always returned as checked=FALSE (eventhough I selected it). If I remove the highlighted (in red) code, then it works fine but that is not the behaviour I need.

 

Not sure why the VF page is not returing the prior disabled value even if it is checked.

 

Posted my working code below. Any sugesstion or soultion would be of great help.

 

Page:

 

<apex:page controller="sampleCon">
<apex:form >
<apex:pageBlock >

<apex:pageBlockButtons >
<apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
</apex:pageBlockButtons>

<apex:pageBlockTable value="{!contacts}" var="c" id="tab">
<apex:column >

<apex:inputCheckbox Id="primarySel" title="Primary" value="{!c.selected}" onclick="deSelectOthers(this,'{!$Component.secSel}')"/>
<script>
if ("{!c.selected}" == "true") {
selectedChkbox = document.getElementById('{!$Component.primarySel}');
}
tableIdCount++;
</script>
</apex:column>
<apex:column >
<apex:inputCheckbox Id="secSel" title="Primary" value="{!c.selectedMany}" onclick="selMany('{!c.selectedMany}')" disabled="{!c.selected}" />
<script>
if ("{!c.selected}" == "true") {
disChkbox= document.getElementById('{!$Component.secSel}');
}
tableIdCount1++;
</script>
</apex:column>

<apex:column value="{!c.con.Name}" />
<apex:column value="{!c.con.Email}" />
<apex:column value="{!c.con.Phone}" />

</apex:pageBlockTable>

</apex:pageBlock>
</apex:form>

<script>
var selectedChkbox;
var disChkbox;

function deSelectOthers(chkBox, sec) {
var tableIdCount = 0;
var tableIdCount1 = 0;
var chkBox2=(document.getElementById(sec));
//alert(chk+'...'+chkBox2.checked);
//alert(document.getElementById(sec).disabled);
chkBox2.disabled=true;
chkBox2.checked=false;
if (chkBox.checked) {
if ((chkBox != selectedChkbox) && (selectedChkbox != null)) {
selectedChkbox.checked = false;
}
if ((chkBox2!= disChkbox) && (disChkbox!= null)) {
//disChkbox.checked=false;
disChkbox.disabled= false;
}
selectedChkbox = chkBox;
disChkbox= chkBox2;


}
}

function selMany(selMany){
//alert(selMany);
}

function dselCk(ck){
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(ck.checked)
inputElem[i].checked = ck.checked;
}
}

function checkAll(cb)
{
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf("checkedone")!=-1)
inputElem[i].checked = cb.checked;
}
}
</script>

</apex:page>

 

 Controller

 

 

public class sampleCon {

public class PublishQuoteException extends Exception {
}

public List<cContact> contactList {get; set;}
List<Contact> selectedContacts = new List<Contact>();
List<Contact> selectedManyContacts = new List<Contact>();

public List<cContact> getContacts(){
if(contactList == null){
contactList = new List<cContact>();

for(Contact c : [select Id, Name, Email, Phone from Contact limit 10]){

if(c.Name == 'Sam Rowe')
contactList.add(new cContact(c, true));
else
contactList.add(new cContact(c, false));
}
}
return contactList;
}

public PageReference processSelected(){

for(cContact cCon : getContacts()){
System.debug('Sec cont...'+cCon.selectedMany);
if(cCon.selected == true){
selectedContacts.add(cCon.con);
}
if(cCon.selectedMany == true){
selectedManyContacts.add(cCon.con);
}
}
if(selectedContacts.size() <= 0)
throw new PublishQuoteException('You must select a primary field');

System.debug('These are the selected Contacts...'+selectedContacts.size());
for(Contact con : selectedContacts){
system.debug(con);
}

System.debug('These are the selected MANY Contacts*** '+selectedManyContacts.size());
for(Contact con : selectedManyContacts){
system.debug(con);
}
return null;
}


public class cContact{
public Contact con {get; set;}
public Boolean selected {get; set;}
public Boolean selectedMany {get; set;}
public Boolean secDis{get; set;}

public cContact(Contact c, Boolean sel){
con = c;
selected = sel;
selectedMany = false;

}
}

}

 

 

 

bob_buzzardbob_buzzard

If a component is disabled, no value is sent back when the page is submitted, regardless of what you do.

 

I encountered the same thing when auto-filling an input text box which I didn't want users to be able to enter values into.  I ended up enabling the textbox and adding some JavaScript to the page which stopped users clicking into it.