I have a VF Page where I wanted to alter an <apex:selectList value="{!dependentSelection}">
's options
dynamically like a dependent picklist with another apex:selectList
. I felt using apex:actionSupport
took too long to update, so I manipulated the dependent apex:selectList
's option
s with jQuery, but in doing so, Visualforce would no longer set dependentSelection
in the controller.
I found a question, Apex:selectList value not getting set in controller, and the solution is to use an HTML and then set it's value to an <apex:hiddenInput value="{!dependentSelection}"/>
.
Also in this VF Page, I denoted many selections with an <apex:inputCheckbox>
and altered their checked
and disabled
properties so that only one selection per section could be chosen and that one selection is always selected:
Example:
<apex:repeat value="{!section.itemList}" var="item">
<tr>
<td><apex:inputCheckbox value="{!item.isSelected}" styleClass="selection" onclick="validateSelection(this);"/></td>
<td><apex:outputField value="{!item.record.Label__c}"/></td>
</tr>
</apex:repeat>
With JavaScript
var j$ = jQuery.noConflict;
// on load
j$('.selection:checked').prop('disabled', true);
function validateSelection(thar){
j$('.selection').each(function(){
if(this == thar)
j$(this).prop('checked', true).prop('disabled', true);
else
j$(this).prop('checked', false).prop('disabled', false);
}
Using this technique worked fine for me in my sandbox, and when I tested this in production with my browser it also worked.
Weird Part # 1
I tried this same technique in a new VF Page (using the same API Version 31) but it did not work. The variables were mysteriously not getting set in the controller (all the inputCheckbox
variables came back as false
in the controller). The culprit turned out to be using the validateSelection
jQuery function and the problem was the same as Apex:selectList value not getting set in controller so the solution was again using a hiddenInput
.
The weird part was the old VF Page's validateSelection
would still work fine without hidden inputs, but the new VF Page's validateSelection
would not work without hidden inputs.
Weird Part #2
In testing, everything would work fine on the old VF Page for my boss using Chrome version 34, but it did not work for our Client using Chrome version 37. There is word that updating to Chrome (beta) version 38 does work for this technique on inputCheckboxes
. Could this be a browser compatability issue? Testing on IE 11 works for my boss, but I still have run my own tests.
Now how can it work for me on one Page but not another?? I'm curious to what the reason is behind this (like what's going on behind the scenes with Visualforce) and if there is any hope that this technique may be supported in the future.
Currently, I believe it is necessary use the hiddenInput
technique now and in the future, but please confirm this is necessary.
Also, I believe the hiddenInput
technique is necessary only if JavaScript is altering an input
's properties or child options
. (Using j$(this).addClass('highlight');
causes no issues.)
.prop('disabled', true)
and test it, does it behave the way that you expect?