Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 How to get all selected check boxes

So I am working on an Angular2 application. I have a table where each record represent a student and includes a checkbox

<input class="mycheckbox" type="checkbox" [value]='student.StudentId'>

At some point user will click on a button that needs to get the value of all selected check-boxes.

I am not sure who should I address this.

One idea is that each student will have a value of checked or not. And this has to be done through two-way bindings.

However this will imply that each time u have to go through all students

Is this the best available option ? And is there something that matches the following JQuery:

$('.mycheckbox:checked').each(function(){
like image 356
Abdelrahman Shoman Avatar asked Dec 11 '15 08:12

Abdelrahman Shoman


People also ask

How do you select all check boxes?

In order to select all the checkboxes of a page, we need to create a selectAll () function through which we can select all the checkboxes together. In this section, not only we will learn to select all checkboxes, but we will also create another function that will deselect all the checked checkboxes.

Does checkbox allow multiple selection?

Checkboxes are objects of a HTML form which behaves like a toggle switch. i.e, a checkbox can be in one of the two states, either checked or unchecked. From a group of checkboxs user can select multiple options.


1 Answers

I recently answered a similar question: https://stackoverflow.com/a/34142740/215945

You could do the following in your template:

<input class="mycheckbox" type="checkbox" [(ngModel)]="student.selected">{{student.StudendId}}

Then, to do something with the selected students:

this.students.filter(_ => _.selected).forEach(_ => { ... })
like image 139
Mark Rajcok Avatar answered Oct 05 '22 22:10

Mark Rajcok