我有一个表,其中两列是表示为字符串的日期。我想根据两个输入过滤表(由两个 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 太不熟悉了,尤其是管道。