Skip to content

Instantly share code, notes, and snippets.

@TRMW
Last active May 3, 2018 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TRMW/99cb93bc366cfaa55e407e93945f8029 to your computer and use it in GitHub Desktop.
Save TRMW/99cb93bc366cfaa55e407e93945f8029 to your computer and use it in GitHub Desktop.
Forced Checkbox Example
import Ember from 'ember';
export default Ember.Controller.extend({
isSelectedAll: false,
isIndeterminate: true,
options: [
Ember.Object.create({
label: 'First',
isSelected: false
}),
Ember.Object.create({
label: 'Second',
isSelected: true,
})
],
actions: {
selectAllDidChange(e) {
const selectAllValue = e.target.checked;
this.get('options').setEach('isSelected', selectAllValue);
this.set('isSelectedAll',selectAllValue);
},
optionDidChange(option, e) {
option.set('isSelected', e.target.checked);
const options = this.get('options');
const selectedOptions = options.filterBy('isSelected');
if (selectedOptions.length === options.length) {
this.set('isSelectedAll', true);
this.set('isIndeterminate', false);
} else if (selectedOptions.length === 0) {
this.set('isSelectedAll', false);
this.set('isIndeterminate', false);
} else {
this.set('isSelectedAll', false);
this.set('isIndeterminate', true);
}
}
}
});
{{input type="checkbox"
checked=isSelectedAll
indeterminate=isIndeterminate
change=(action "selectAllDidChange")}}
Selected All
<hr>
{{#each options as |option|}}
<div>
{{input type="checkbox"
checked=option.isSelected
change=(action "optionDidChange" option)}}
{{option.label}}
selected: {{option.isSelected}}
</div>
{{/each}}
{
"version": "0.13.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment