Nam
Nam

Reputation: 67

ember js get multiple checkbox value

My templates:

<form name="spam_list" {{action 'spamDecide' on='submit'}}>
    {{#each model as |spam|}}
    <tr>
        <td>{{input type="checkbox" name="trash[]" checked=spam.isCheck}}</td>
    </tr>
    {{/each}}                       
<button {{action 'checkAll'}}>Check All</button>
</form>

My routes:

model(){
        // return 10 spams
    },

My controller:

actions:{
        spamDecide: function(model){
             var isCheck = model.getEach('isCheck');
             console.log(isCheck);
        },

        checkAll: function(model){
            var model = this.get('model');
            model.setEach('isCheck',true);
        },
    }

Currently action checkAll is work fine, set all checkbox into checked. My question is how can I know which checkbox is checked when I manually check them.
For example I set checkbox 1 5 7 is checked, then get the equivalent spam.id with each check?

Upvotes: 0

Views: 506

Answers (1)

locks
locks

Reputation: 6577

If I read correctly, what you're looking for is to get a list of checked objects in spamDecide. You should be able to do it along these lines:

actions:{
  spamDecide: function(model){
    var checkedModels = model.filterBy('isCheck');
    console.log(checkedModels);
  },
}

While Array#getEach (alias for Array#mapEach) returns an array of each item's isCheck property, filterBy returns the items that have the isCheck property set to true.

If you want the ids you can then do checkedModels.mapBy('id').

I hope that was helpful!

Upvotes: 3

Related Questions