Angular - 将* ngIf与* ngFor一起使用到表中的特定行

时间:2017-06-05 12:26:57

标签: angular filter pipe

我对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>
&#13;
&#13;
&#13;

这里是我的tracker.component.ts

&#13;
&#13;
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;
    }
}
&#13;
&#13;
&#13;

再次感谢!

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

1 个答案:

答案 0 :(得分:1)

您无需为此创建指令:

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

现在,只有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>