简体   繁体   English

带有 PrimeNg/Angular 的 P-checkbox 获取所选元素的列表

[英]P-checkbox with PrimeNg/Angular getting list of selected elements

i'm iterating over an array of elements with p-table我正在使用 p-table 迭代一组元素

Here is mu HTML这是 mu HTML

    <p-table selectionMode="single" [value]="users" styleClass="p-datatable-customers p-datatable-gridlines p-datatable-striped p-datatable-sm"
        [scrollable]="true" >
        <ng-template pTemplate="header">
          <tr>
            <th>UserName</th>
            <th>Selected</th>
          </tr>
        </ng-template>

        <ng-template pTemplate="body" let-user>
          <tr>
            <td>{{user.name}}</td>
            <td>
              <p-checkbox formControlName="userAdd" name="userAdd" [value]="user"></p-checkbox>
            </td>
          </tr>
        </ng-template>

      </p-table>
      

In my Ts在我的 Ts

    createForm(): void {
    this.addUserForm = this.fb.group({
    userAdd: this.fb.control(''),
    }); 

When extracting value of my selected users i use :在提取所选用户的价值时,我使用:

    const users = this.addUserForm.get('userAdd').value;

But i got just one element (the last one)但我只有一个元素(最后一个)

here is my selected element这是我选择的元素

    value: Array(1)
    0:
    id: 1
    name: "toto"
    

Now i want to retrieve all selected elements in one array.现在我想检索一个数组中的所有选定元素。

通过在我的 p-checkbox 中添加 [(ngModel)]="selectedValues" 解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM