Ronni Egeriis Persson
Ronni Egeriis Persson

Reputation: 2289

Ember.js: Getting checked checkboxes

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

Upvotes: 0

Views: 857

Answers (1)

tikotzky
tikotzky

Reputation: 2592

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

Upvotes: 1

Related Questions