Matt
Matt

Reputation: 1811

EmberJS checkbox set value

According to the ember documentation, I can do this:

{{input type="checkbox" name="fruits[]" checked=isAdmin}}

This works fine, however... my checkbox, although 'on or off' but consider the following:

[ ] Apples
[ ] Blueberries
[ ] Oranges

Let's say the 'value' for each one of those is the label... so if someone were to check all 3, it would pass this as an array:

["Apples","Blueberries","Oranges"]

However, this isn't what happens...I get:

["true","true","true"]

Upvotes: 0

Views: 365

Answers (1)

Bek
Bek

Reputation: 3207

if you want checkbox values to be in array form then you have to create computed property which will depend on checkbox properties.

selectedFruits: Ember.computed('isBananas', 'isBlueberries', 'isOranges', function() {
  var values = [];
  var isBanana = this.get('isBanana');
  var isOranges = this.get('isOranges');
  var isBlueberries = this.get('isBlueberries');
  if (isBanana) { values.push('bananas'); }
  if (isBlueberries) { values.push('blueberries'); }
  if (isOranges) { values.push('oranges'); }
  return values;
});

or use ember addon ember-multiselect-checkboxes instead

{{multiselect-checkboxes options=fruits selection=selectedFruits}}

where selectedFruits === ['bananas','oranges'] selected items in array

Upvotes: 1

Related Questions