Angular - 将 *ngIf 和 *ngFor 用于表中的特定行

rca*_*a02 0 pipe filter angular

我对 Angular 还很陌生,我正在尝试制作一个管道来从我的表格中过滤项目,但我不太确定如何去做。我试图只显示 EmpKey = empInfo[selectedEmployee].EmpKey 的表字段。任何帮助将不胜感激!提前致谢!

这是我的 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>
Run Code Online (Sandbox Code Playgroud)

这是我的 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

再次感谢!

编辑澄清- 用户将从下拉列表中选择一名员工,这将为我提供所选员工的索引。从那里,我想通过使用 empInfo[selectedEmployee].EmpKey 中的值过滤掉不包含该 EmpKey 的结果。在表格中,有许多不同的EmpKeys,并且只要选择用户,我只希望在选择不同的字段时共享empKey的字段被打印并更改为另一名员工。

Ced*_*Ced 7

您不需要为此创建指令:

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

现在一行只会显示如果condition


所以你的代码变成

    <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>
Run Code Online (Sandbox Code Playgroud)