【问题标题】:Angular - Using *ngIf with *ngFor to specific rows in tableAngular - 使用 *ngIf 和 *ngFor 到表中的特定行
【发布时间】:2017-06-05 21:15:39
【问题描述】:

我对 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>

这是我的 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;
    }
}

再次感谢!

为澄清而编辑 - 用户将从下拉列表中选择一名员工,这将为我提供所选员工的索引。从那里,我想通过使用 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.

【问题讨论】:

  • 上面的代码目前只选择了一名员工。

标签: angular filter pipe


【解决方案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>

【讨论】:

  • 抱歉,我的问题可能不够清楚。用户将从下拉列表中选择一名员工,这将为我提供所选员工的索引。从那里,我想过滤掉不包含该 EmpKey 的结果。会有很多不同的 EmpKey 被选中
  • @rcarc 是什么阻止您使用我的代码执行此操作?我想我错过了什么
  • 每当我使用您提供的代码时,它总是会打印出整个表格,并且每当我从组合框中选择不同的员工时都不会发生任何变化。
  • @rcarc 你放在那里的条件是什么?
  • *ngIf="pto.EmpKey = empInfo[selectedEmployee].EmpKey."再想一想,我不相信这会奏效,但我不太确定我会放在这里。
猜你喜欢
  • 2016-09-17
  • 1970-01-01
  • 2020-09-28
  • 2020-06-21
  • 2018-06-17
  • 2019-05-17
  • 2018-07-16
  • 1970-01-01
  • 2019-09-18
相关资源
最近更新 更多