Флажок в таблице для выбора строки таблицы в угловых - PullRequest
2 голосов
/ 14 марта 2019

У меня есть таблица в Angular 6, там есть флажок?То, что я хочу сделать, это выбрать строку таблицы, то есть флажок, затем нажать кнопку выбора, и это будет отправлено.Мне было интересно, должен ли стол быть завернут в <form>.Структура моего HTML пока такая, но она не работает:

 <form [formGroup]="assetForm" (ngSubmit)="onSubmit()">
     <table class="table table-striped table-hover mb-10">
         <thead>
            <tr>
              <th>Number</th>
              <th>Sev</th>
              <th>Phone</th>
            </tr>
          </thead>
          <tbody>
             <tr *ngFor="let incident of data">
               <td>
                  <label class="form-radio">
                    <input type="radio" name="id_number" value="id_number">
                    <i class="form-icon"></i>{{incident.number}}
                  </label></td>
                <td>{{incident.sev}}</td>
                <td>{{incident.phone}}</td>
              </tr>
            </tbody>
       </table>
       <button class="btn btn-primary" [disabled]="!Form.valid" type="submit">Select</button>
  </form>

Ts.file

onSubmit() {
    if (this.assetForm.invalid) {
      this.assetForm.setErrors({ ...this.assetForm.errors, 'required': true });
      return;
    }
    this.uploading = true;
    this.service.post(this.assetForm.value).subscribe((response: any) => {
      console.log(response);//On success response

    }, (errorResponse: any) => {
      console.log(errorResponse); //On unsuccessful response
      this.error = true;
      this.uploading = false;
    });
  }

1 Ответ

0 голосов
/ 14 марта 2019
<tr *ngFor="let incident of data">
    <td>
        <label class="form-radio">
           <input type="radio" name="id_number" value="id_number" [(ngModel)]="incident.checked">
           <i class="form-icon"></i>{{incident.number}}
        </label>
    </td>
    <td>{{incident.sev}}</td>
    <td>{{incident.phone}}</td>
</tr>

Попробуйте, надеюсь, это сработает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...