我有一个表,其中两列是表示为字符串的日期。我想基于两个输入(由两个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;
});
}
}
我相信我的问题在我的转换功能中是不正确的语法/功能。我已经看到了类似的问题like this,但我似乎无法根据自己的需要对其进行格式化,而且我对Angular2太熟悉了,尤其是管道。
答案 0 :(得分:1)
arg不是数组,只是之后的第一件事:
export class SearchPipe implements PipeTransform {
transform(value, firstDate , secondDate , arg3 , arg4 ){
return value.filter(row => {
return row.tStartDate >= firstDate && row.tEndDate <= secondDate;
});
}
}
答案 1 :(得分:0)
我希望这可以帮助您的示例,因为它是我的问题的解决方案。
尝试重用async
buikd-in管道,以便在承诺解析后通知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
类型 - 您需要承诺作为返回类型
var theData: Promise<DataTable> = null;
this.theData = this.dataService.getTable();
因此,使用此方法,您的转发器不会开始处理您的数据,直到您的承诺无法解决。在此阶段,您将获得数据,而searchDates
管道不会导致空数据异常。
以下是有关此管道https://angular.io/docs/ts/latest/api/common/index/AsyncPipe-pipe.html
的更多详细信息