简体   繁体   English

以角度获取多个复选框值

[英]get multiple checkbox value in angular

I pulled data from getListOfCurrentSubtenants and populate it as you can see on the screenshot, user can select by clicking the checkbox.我从 getListOfCurrentSubtenants 中提取数据并填充它,如屏幕截图所示,用户可以通过单击复选框进行选择。 how do we get all the data that user check as a single object?我们如何将用户检查的所有数据作为单个对象获取?

So if user click 5 boxes then the result would be a single array of objects with all the selected items.因此,如果用户单击 5 个框,则结果将是包含所有选定项目的单个对象数组。

sample object样本对象

[
    {
        "subtenantName": "Urgent Care MSO, LLC",
        "currentLeaseExpiration": "04/30/2023"
    },
    {
        "subtenantName": "Laboratory Corporation of America",
        "currentLeaseExpiration": "10/10/2028"
    }
]

html code html代码

<div *ngIf="currentSubtenants.length > 0" class="deal-form-header-labels" style="padding-top: 5px;">Current Subtenants</div>
          <div *ngFor="let subtenant of currentSubtenants; let i = index;" class="subtenant-form-btn-group">
            <div class="deal-form-btn-group-radio" fxLayout="row" fxLayoutAlign="space-between center" >
              <div class="pharmacy-checkbox">
                <mat-checkbox
                 color="primary"
                 [(ngModel)]="dealDispositionFormFields.currentSubtenants"
                 (change)="changeCurrentSubtenants($event,subtenant)"
                 style="margin-left:10px;">
                 <div class="deal-text-label">
                   {{subtenant.subtenantName}}
                </div>
                </mat-checkbox>  
                </div>
            </div>
            <div >
              <div class="flex-column">
                <mat-label>Current Lease Expiration</mat-label>
                <div class="property-assignments-email secondary-text">{{subtenant.currentLeaseExpiration}}</div>
              </div>
            </div>
          </div>
        </div>

code代码

changeCurrentSubtenants(e:any,rowData:any){    
    if(e.checked){
      console.log("rowData" , rowData)
    }
  }

    getListOfCurrentSubtenants() {
          this.partnerRoleIsInProgress = true;
           this._dealService.getCurrentSubtenants(this.transactionData.propertyId)
            .pipe(
              finalize(() => this.partnerRoleIsInProgress = false),
            ).subscribe({
              next: (res) => {
                if (res.isSuccess) {
                  this.currentSubtenants = res.data;
                  console.log("res" , this.currentSubtenants)
                }
              },
              error: err => this._notificationService.showError(err),
              complete: noop
            });
        }

在此处输入图片说明

Without refactoring your code to much, you could add and remove these items in a array.无需过多重构代码,您就可以在数组中添加和删除这些项目。

interface Data {
  subtenantName: string;
  currentLeaseExpiration: string;
}


results: Data [] = [];
changeCurrentSubtenants(e: any, rowData: Data) {
  if (e.checked) {
    this.results.push(rowData);
  } else {
    this.results = this.results.filter(item => item.subtenantName === rowData.subtenantName);
  }
}

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

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