0

How do I get the checkboxes that have been checked, in a case where I iterate over values to create a variable range of checkboxes?

HBS:

<ul>
{{#each item in model}}
  <li><label>{{input type="checkbox"}} {{item}}</label></li>
{{/each}}
</ul>

Route:

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return ['red', 'yellow', 'blue'];
  },
  actions: {
    iHaveSelected: function() {
      // Get checked items
    }
  }
});

http://emberjs.jsbin.com/tucoka/1/edit

1 Answer 1

1

In order to track if a checkbox is checked you need to bind the checked attribute to a property on a controller.
In the below example the checkbox would be bound to the isChecked property.

{{input type="checkbox" checked=isChecked}}

In your case you were looping over an array, the way to do it would be to set an itemController on the ArrayController.
The itemController would maintain the isChecked state for each item in the ArrayController. You would then be able to filter the ArrayController by which items are checked.

I created a bin with an example here: http://emberjs.jsbin.com/vutezo/1/edit

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, that looks great. Can I nest this within another view?
i'm not sure exactly what you mean but you should be able to put a nest checkbox where in any view
Say the colors is a property on the model (or controller), I suppose I would have to isolate the colors property to be handled by an ArrayController? emberjs.jsbin.com/zujobe/1/edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.