1

我有一个表,其中两列是表示为字符串的日期。我想根据两个输入过滤表(由两个 ngb-datepickers 填充,默认值为 null)。使用自定义管道我目前收到控制台错误Cannot read property '0' of undefined以及我的表格没有呈现到页面,没有任何按钮起作用,并且表单验证不起作用。我将自定义管道导入到 app.module.ts 中的声明中。包含最少的代码以显示整个范围,让我知道是否有任何令人困惑或包含更多内容。

mainpage.component.html:

<div>
  <label>Start Date:</label>
  <input type="text" [(ngModel)]="startDateValue">
</div>
  <label>End Date:</label>
  <input type="text" [(ngModel)]="endDateValue">
</div>
//'let idx=index' and 'let even=even' are used to change color of the rows but I took out that code. The 'onClick' function just takes the row and uses an EventEmitter to output it.
<tr *ngFor="let dPoint of theData | searchDates:startDateValue:endDateValue; let idx=index; let even=even;" (click)="onClick(dPoint, idx)">
  <td>{{dPoint.tDataPoint}}</td>
  <td>{{dPoint.tICCP}}</td>
  <td>{{dPoint.tStartDate}}</td>
  <td>{{dPoint.tEndDate}}</td>
</tr>

mainpage.component.ts:

@Component({
  selector: 'main-page',
  styleUrls: ['../app.component.css'],
  templateUrl: 'mainpage.component.html',
  providers: [DataTableService, DatePipe]
})

export class MainPageComponent implements OnInit {
  secondForm : FormGroup;
  theData:DataTable[] = [];

  constructor(fb: FormBuilder, private datePipe: DatePipe, private dataService: DataTableService, private cdRef:ChangeDetectorRef){
    this.secondForm = fb.group({
      'startDate' : [null, Validators.required],
      'endDate' : [null, Validators.required]
    }, {validator: this.endDateAfterOrEqualValidator})
  }

  getTable(): void {
    this.dataService.getTable().then(theData => this.theData = theData);
    this.cdRef.detectChanges();
  }
}

搜索-pipe.ts:

import { Pipe, PipeTransform } from "@angular/core";

@Pipe({
  name: "searchDates"
})

//if I comment out the code inside the transform function, I get no console errors, all functionality previously mentioned that stopped working now does again, but my entire table still does not render
export class SearchPipe implements PipeTransform {
  transform(value, args?){
    let firstDate = new Date(args[0]);
    let secondDate = new Date(args[1]);
    //let [minDate, maxDate] = args;
    return value.filter(row => {
      return row.tStartDate >= firstDate && row.tEndDate <= secondDate;
    });
  }
}

我相信我的问题是我的转换函数中的语法/功能不正确。我见过类似的问题但我似乎无法根据自己的需要对其进行格式化,而且我对 Angular2 太不熟悉了,尤其是管道。

4

2 回答 2

1

arg 不是数组,只是之后的第一件事:

export class SearchPipe implements PipeTransform {
  transform(value, firstDate , secondDate , arg3 , arg4 ){

    return value.filter(row => {
      return row.tStartDate >= firstDate && row.tEndDate <= secondDate;
    });
  }
}
于 2016-12-29T13:50:21.067 回答
0

我希望这对您的示例有所帮助,因为它可以解决我的问题。

尝试重用async内置管道以通知 Angular 仅在承诺解决后才继续进行表单构建。

您的示例可能如下所示:

<tr *ngFor="let dPoint of theData | async | searchDates:startDateValue:endDateValue; let idx=index; let even=even;" (click)="onClick(dPoint, idx)">
  <td>{{dPoint.tDataPoint}}</td>
  <td>{{dPoint.tICCP}}</td>
  <td>{{dPoint.tStartDate}}</td>
  <td>{{dPoint.tEndDate}}</td>
</tr>

但首先你需要改变你的theData类型——你需要 promise 作为返回类型

var theData: Promise<DataTable> = null;
this.theData = this.dataService.getTable();

因此,使用这种方法,您的转发器不会开始处理您的数据,直到您的承诺不会得到解决。在这个阶段,您将拥有您的数据,并且您的searchDates管道不会导致空数据异常。

这是有关此管道的更多详细信息https://angular.io/docs/ts/latest/api/common/index/AsyncPipe-pipe.html

于 2016-12-29T13:47:36.123 回答