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(){
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.
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.
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(_ => { ... })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With