6

だから私はAngular2アプリケーションに取り組んでいます。各レコードが学生を表し、チェックボックスを含むテーブルがあります

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

ある時点で、ユーザーは、選択されたすべてのチェックボックスの値を取得する必要があるボタンをクリックします。

これを誰に話せばいいのかわかりません。

1 つのアイデアは、各学生がチェックされているかどうかの値を持つことです。そして、これは双方向バインディングを介して行う必要があります。

ただし、これは毎回すべての学生を通過する必要があることを意味します

これは利用可能な最良のオプションですか? そして、次のJQueryに一致するものはありますか:

$('.mycheckbox:checked').each(function(){
4

6 に答える 6

8

最近、同様の質問に答えました: https://stackoverflow.com/a/34142740/215945

テンプレートで次のことができます。

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

次に、選択した生徒に対して何かを行うには:

this.students.filter(_ => _.selected).forEach(_ => { ... })
于 2015-12-11T20:44:53.953 に答える
2

同じことを行う別の方法は次のとおりです。

.html

<button value="all" (click)="bulk('all')">ALL</button>
<button value="none" (click)="bulk('none')">NONE</button>

<div *ngFor="#students of student_list #i=index">
  <input type="checkbox" value={{students.id}} class="checkedStudent"   
  (change)="checkedStudents(students.id)" id={{students.id}}>
</div>

.ts ファイル内

abc:Array<any>= [];
checkedStudents(value) {
        console.log(value);
        if ((<HTMLInputElement>document.getElementById(value)).checked === true) {
            this.abc.push(value);
        }
        else if ((<HTMLInputElement>document.getElementById(value)).checked === false) {
            let indexx = this.abc.indexOf(value);
            this.abc.splice(indexx,1)
        }
        console.log(this.abc)
    }

// for Bulk actions

bulk(a) {
        // console.log(a);
        if (a == "all") {
            console.log("yes all");
            this.abc = [];
            for (let i = 0; i < this.student_list.length; i++) {
                (<HTMLInputElement>document.getElementsByClassName("checkedStudent")[i]).checked = true;
                this.abc.push(this.student_list[i].id);
            }
        }
        if (a == "none") {
            for (let i = 0; i < this.student_list.length; i++) {
                (<HTMLInputElement>document.getElementsByClassName("checkedStudent")[i]).checked = false;
            }
            this.abc = [];
        }
        console.log(this.abc);
    }
于 2016-02-04T19:16:42.117 に答える
1

ボタンに条件を追加するだけで、チェックボックス フィールドの「値」を次のようにチェックすることを忘れないでください。

<form 
    #f="ngForm"  
    (ngSubmit)="onSubmit(f.value)" >

    <div class="form-group">

        <h2>Enter Email for Newsletter :</h2> <br/>

        <input 

            #registrationemail="ngForm"
            ngControl="registrationemail" 

            placeholder="Email Address"
            required type="email" 
            class="form-control" 
            pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$"  />

        <div *ngIf="registrationemail.touched && registrationemail.errors" class="alert alert-danger">
            <div  *ngIf="registrationemail.errors.required" >An Email is required</div>
            <div  *ngIf="registrationemail.errors.pattern">Email is invalid</div>
        </div>

    </div>

    <div class="form-group">


        <input 
        id="accepttermsconditions"
        #accepttermsconditions="ngForm"
         ngControl="accepttermsconditions" 
        type="checkbox" 
        checked/>
        <label for="accepttermsconditions">Accept Terms and Conditions </label> 
        </div>

    <button
       [disabled]="!f.valid || 
        !accepttermsconditions.value"
      class="btn btn-primary" 
      type="submit">Submit </button>

</form>
于 2016-05-28T15:37:20.040 に答える
0

PrimeNG DataTable には、これが組み込み機能として備わっています。selectionMode を multiple に設定するだけで、チェック ボックス ベースの選択を取得できます。ライブ デモはhttp://www.primefaces.org/primeng/#/datatableselectionにあります。

于 2016-07-26T18:36:11.170 に答える
0

前のページと同様の方法で「formbuilder」を使用して行うこともできます。次のように投稿します。

import {Component, OnInit} from '@angular/core';
import { FORM_DIRECTIVES, FormBuilder, ControlGroup } from '@angular/common';

@Component({
    selector: 'registration-form',
    directives: [FORM_DIRECTIVES],
    template: `

    <form 
    [ngFormModel]="myForm" 
        (ngSubmit)="onSubmit(myForm.value)" 
        class="ui form"> 

          <div class="form-group">
     <label for="registrationemailInput">EMail Address</label>  
     <input type="email"  
            required
            id="registrationemailInput" 
            placeholder="email address"  
            [ngFormControl]="myForm.controls['registrationemail']"
            class="form-control" 
            pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$"
             />


                <div *ngIf="myForm.controls['registrationemail'].touched && myForm.controls['registrationemail'].errors" class="alert alert-danger">
            <div  *ngIf="myForm.controls['registrationemail'].errors.required" >An Email is required</div>
            <div  *ngIf="myForm.controls['registrationemail'].errors.pattern">Email is invalid</div>
        </div>

            </div>

            <div class="form-group">
            <label for="termsandconditions">Accept Terms &amp; Conditions</label>
      <input  id="termsandconditions"
                type="checkbox"
                checked      
                [ngFormControl]="myForm.controls['accepttermsconditions']" 
                id="accepttermsconditions"
      />
      </div>

      <button 

       [disabled]="!myForm.valid || !myForm.controls['accepttermsconditions'].value"

            class="btn btn-primary"  
            type="submit">Submit</button>
     </form>`
})
export class FormbuilderComponent implements OnInit {

    myForm: ControlGroup;

    constructor(fb: FormBuilder) {
        this.myForm = fb.group({
            'registrationemail': ['test'],
            'accepttermsconditions': [true]
        })
    }

    ngOnInit() {
    }


    onSubmit(form: any): void {
        console.log('you submitted value:', form);
    }
}
于 2016-05-28T16:26:23.937 に答える