0

I have a table with one of the columns being a checkbox. I want to be able to pass all the checked items values into a list in another apex controller.

VFP

<apex:repeat value="{!data}" var="Results">
<tr>
    <td style="text-align: center" width="4%"><input type="checkbox" name="bid" 
   value="{!Results.Id}" /></td>
</tr>
</apex:repeat>

Apex:

public class UDQuoteTemp 
{
    public List<Id> Result_Ids {get; set;} //List of all the checked Ids

    public UDQuoteTemp(ApexPages.StandardController controller)
    {
        //SOQL query all the ids from the list that is passed in.
    }
}
  • 2
    Traditionally one does this using a wrapper class pattern, although it could certainly be done with, e.g., JS remoting. Have you looked at using a wrapper class? – David Reed Oct 19 '18 at 15:30
  • Not sure I fully understand what you're trying to do here, but it looks like you're assigning a checkbox value to a ID? Checkbox values have to be stored as Booleans, not IDs. – Charlie123 Oct 19 '18 at 16:47
0

I'm sure somebody will have a better answer than this, but here's how I handle this.

I find it is easier to bring the list in as a String instead, but assign a Javascript Array to the String. This way, you end up with a comma separated string which is easy to split.

Apex:

public class CheckboxCTRL {
  public List<String> objects{get;set;}

  public String selected{get;set;}

  public CheckboxCTRL(){
    objects = new List<String>{'test1','test2','test3','test4','test5'};
    selected = '';
  }
  public void doStuff(){
    List<String> the_list = selected.split(',');
    System.debug(the_list);
  }
}

The Javascript simply adds/removes a property from the object as you check/uncheck boxes. Each time, it prints the list of keys in the hidden input.

Realistically, you could only print the keys on the submit press, which would be more efficient.

Visualforce:

<apex:page controller = "CheckboxCTRL" sidebar="false">
  <script>
    var checked = {}; // object to store clicks

    function handleClick(event){

      var checkbox = event.target;
      if(checkbox.checked == true){

        // store ID
        checked[checkbox.getAttribute('data-id')] = 'true'; 

      } else {

        // delete ID
        delete checked[checkbox.getAttribute('data-id')];

      }

      // don't forget to use the component ID
      var text_field = document.getElementById('{!$Component.theForm.hiddenInput}');

      // Built a list of the keys
      var key_list = [];
      for (var key in checked) {
        if (checked.hasOwnProperty(key)) {
            key_list.push(key);
        }
      }

      // set field value
      text_field.value = key_list;
    }
  </script>
  <apex:form id="theForm">
    <table>
      <apex:repeat var="obj" value="{!objects}">
        <tr>
          <td>{!obj}<input type="checkbox" data-id="{!obj}" onclick="handleClick(event)"/></td>
        </tr>
      </apex:repeat>
    </table>
    <apex:inputHidden id="hiddenInput" value="{!selected}"/>
    <apex:commandButton action="{!doStuff}" value="Do Stuff"/>
  </apex:form>
</apex:page>

It's important to note that using $Component in VisualForce requires you to list the hierarchy of components to what you are trying to reference. While I only had a form and the input, if you embed that in a pageBlock, you'd have to reference the pageBlock ID. See the Salesforce Documentation on $Component here.

Hope this makes sense. It worked perfectly in my developer org, printing in the log whatever checkboxes I clicked.

  • I tried your code but get a console error of: Uncaught TypeError: Cannot set property 'value' of null – Alexander Atkinsoon Oct 22 '18 at 9:21
  • @AlexanderAtkinsoon That probably means you had an issue with the $Component.ID syntax to access visualforce components. Notice that in code, I reference {! $Component.theForm.hiddenInput}, this is because the apex:inputHidden component is within a apex:form tag with ID 'theForm' Make sure you reference all levels of nested visualforce components, so that if you put the form within a pageblock, you need to reference it as $Component.PageblockID,FormID,InputID These can sometimes be 'finicky' in my experience, so you may need to play around with what component ids you use to describe it. – stackasaur Oct 22 '18 at 12:52

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.