简体   繁体   English

Angular-对表中的特定行使用* ngIf和* ngFor

[英]Angular - Using *ngIf with *ngFor to specific rows in table

I'm pretty new to Angular and I'm trying to make a pipe to filter items out of my table and am not quite sure how to go about that. 我对Angular来说还很陌生,我试图制造一个管道来从表中过滤掉项目,但不确定如何去做。 I'm trying to only display the table fields where the EmpKey = empInfo[selectedEmployee].EmpKey. 我试图只显示EmpKey = empInfo [selectedEmployee] .EmpKey的表字段。 Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks in advance! 提前致谢!

Here's my tracker.component.html 这是我的tracker.component.html

 <div class="row"> <div [ngClass]="{'col-xs-12':isHidden === true, 'col-xs-7': isHidden !== false}"> <button class="form-control" style="width:150px;" (click)="toggleSummary()">Open Summary</button> <select id="empName" [(ngModel)]="selectedEmployee"> <option selected="selected" disabled>Employee Name...</option> <option *ngFor="let emp of empInfo; let i = index" [ngValue]="i">{{i}} - {{emp.EmpID}}</option> </select> <select id="PTOtype"> <option selected="selected" disabled>Type of PTO...</option> <option value="PTO">PTO</option> <option value="ETO-Earned">ETO - Earned</option> <option value="ETO-Used">ETO - Used</option> <option value="STDLTD">STD/LTD</option> <option value="Uncharged">Uncharged</option> </select> <button class="form-control" style="width: 150px;" (click)="nextEmployee()">Next</button> <button class="form-control" style="width: 150px;" (click)="previousEmployee()">Previous</button> <h2 *ngIf="empInfo && empInfo.length > selectedEmployee">{{empInfo[selectedEmployee].FirstName}} {{empInfo[selectedEmployee].EmpKey}}</h2> <table class="table"> <thead> <tr> <th>Date</th> <th>Full/Half</th> <th>Hours</th> <th>Scheduled?</th> <th>Notes</th> <th>In P/R?</th> </tr> </thead> <tbody> <tr *ngFor="let pto of ptoData> <td>{{pto.date | date: 'MM/dd/y'}}</td> <td>{{pto.EmpKey}}</td> <td>{{pto.fullhalf}}</td> <td>{{pto.hours}}</td> <td>{{pto.scheduled}}</td> <td>{{pto.notes}}</td> <td>{{pto.inPR}}</td> </tr> </tbody> </table> </div> <div *ngIf="isHidden" class="col-xs-5"> <pto-summary [selectedEmployee]="selectedEmployee"></pto-summary> </div> </div> 

and here's my tracker.component.ts 这是我的tracker.component.ts

 import { Component, OnInit, Input } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { PTODataService } from './pto-data.service'; import { PTOData } from './pto-data'; import { EmpInfoService } from './emp-info.service'; import { EmpInfo } from './emp-info'; @Component({ selector: 'pto-tracker', templateUrl: `./tracker.component.html`, styleUrls: ['./tracker.component.css'] }) export class TrackerComponent implements OnInit{ empInfo: EmpInfo[]; ptoData: PTOData[]; isHidden: boolean = false; public selectedEmployee: number; constructor( private empInfoService: EmpInfoService, private ptoDataService: PTODataService) { } getEmpInfo(): void { this.empInfoService.getEmpInfos().then( empInfo => this.empInfo = empInfo ); } getPTOData(): void { this.ptoDataService.getPTODatas().then( ptoData => this.ptoData = ptoData ); } ngOnInit(): void { this.getEmpInfo(); this.getPTOData(); } toggleSummary(): void { this.isHidden = !this.isHidden; } nextEmployee(): void { this.selectedEmployee = this.selectedEmployee+1; } previousEmployee(): void { this.selectedEmployee = this.selectedEmployee-1; } } 

Thanks again! 再次感谢!

EDIT for Clarification - The user will select an employee from a dropdown list which will give me the index of the employee that was chosen. 澄清说明 -用户将从下拉列表中选择一名雇员,这将为我提供所选雇员的索引。 from there, I want to filter out the results that don't contain that EmpKey by using the value from empInfo[selectedEmployee].EmpKey. 从那里,我想使用empInfo [selectedEmployee] .EmpKey中的值来筛选出不包含该EmpKey的结果。 In the table, there are many different EmpKeys and whenever a user is selected, I only want the fields that share that EmpKey to be printed out and change to another employees whenever a different one is selected. 在表中,有许多不同的EmpKey,并且每当选择一个用户时,我只希望共享该EmpKey的字段被打印出来,并且每当选择一个不同的雇员时就更改为其他雇员。

You don't need to create a directive for that : 您无需为此创建指令:

    <ng-container *ngFor="let pto of ptoData">
       <tr *ngIf="condition" >
          <td>{{pto.date | date: 'MM/dd/y'}}</td>
          ...
       </tr>
    </ng-container>

Now a row will only display if condition . 现在,仅在condition显示一行。


So your code becomes 所以你的代码变成

    <ng-container *ngFor="let pto of ptoData">
       <tr *ngIf="pto.EmpKey === empInfo[selectedEmployee].EmpKey" >
          <td>{{pto.date | date: 'MM/dd/y'}}</td>
          ...
       </tr>
    </ng-container>

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

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