5

I have a list of booleans, and some checkboxes. When a checkbox is clicked, a corresponding index in the boolean list is changed to true/false. My issue is that if the checkboxes are clicked too fast, sometimes the values do not change (for example, if a checkbox was initially true and I clicked it 2 times really fast, it might be false in the end). What could be causing this issue? Is there some sort of lock/mutex mechanism I could use? Here is a condensed version of the code:

Apex Class

    public List<Boolean> myCheckboxValues{get;set;}

       public void checkboxClick(){
    this.myCheckboxValues[myIndex] = !this.myCheckboxValues[myIndex];
    }

Visual Force Page

    <apex:inputCheckbox >
      <apex:actionSupport event="onclick" action="{!checkboxClick}" reRender="otherPartOfMyCode">
        <apex:param name="indexOne" value="0" assignTo="{!myIndex}"/>
       </apex:actionSupport>
    </apex:inputCheckbox>
    /* Imagine a few other identical codeblocks but value is changed to the other indexes */
  • can you try wrapping the <apex:inputcheckbox> within the <apex:actionregion> tag – Anurag A Jul 21 '16 at 14:44
  • 1
    A List<Boolean> is rarely the optimal data structure. Are you just trying to include a selection mechanism for a List<SObject>? – Adrian Larson Jul 21 '16 at 14:49
  • 1.) Anurag: actionregion did not work. 2.) Adrian: This list will be used to decide whether certain things will render on a following page. – Aerin C Jul 21 '16 at 15:10
7

A good way to do that is to skip the method and use the basic visualforce setters.

<apex:inputCheckbox value="{!myCheckboxValues[0]}">
     <apex:actionSupport event="onchange" reRender="otherPartOfMyCode">
</apex:inputCheckbox>
  • 1
    @AerinC Keep in mind that with enough latency, even this might not always work. Personally, I'd recommend using apex:actionStatus to block the UI from further updates while the view state is being processed. You can just put a fixed div over the entire page so the user won't actually be able to interact with the elements until the update completes. – sfdcfox Jul 21 '16 at 16:12
7

This is primarily due to network latency. When using apex:actionSupport, an ajax call is initiated to the server. These calls are non-blocking, meaning you can initiate several, completely independant calls at a time. The latency comes into play when they don't necessarily return in the same order they were submitted.

The best way to take care of this is to use an apex:actionStatus tag that can be set to signify to the user that we are doing something. You could also use the onSubmit and onComplete attributes of the apex:actionSupport tag to enter some Javascript to disable the checkboxes until you get a response back. You would want to disable all of them because your entire viewstate is submitted each time, so the same issue will happen if you move fast between checkboxes as well as edits to the same one.

  • Fernando's solution worked, but thank you for the explanation on why this is happening :) – Aerin C Jul 21 '16 at 15:53

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.