我有一个表,我使用自定义管道成功过滤。过滤器基于两个输入,它们是一个表格。我想添加的功能是在点击提交按钮之前不进行过滤。所以它更像是一个搜索按钮。我发现了很多关于管道的信息,但没有关于按钮点击时激活它们的信息。
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>
//the button that will execute the filtering
<button type="submit" class="btn icon-search" [disabled]="!secondForm.valid" (click)="submit(secondForm.value)"></button>
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();
}
submit(value: any){
//where I'd want to trigger the filtering/pipe
}
}
搜索pipe.ts:
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({
name: "searchDates"
})
export class SearchPipe implements PipeTransform {
transform(value, minDate , maxDate){
return value.filter(row => {
return row.tStartDate >= minDate && row.tEndDate <= maxDate;
});
}
}
答案 0 :(得分:4)
您可以考虑删除管道,而只是在用户单击按钮时自行过滤数据。
首先,定义表示过滤结果的第二个属性
let theFilteredData: DataTable[]
更改绑定以绑定到FilteredData:
*ngFor="let dPoint of theFilteredData;" //rest of *ngFor not included
在提交功能中:
this.theFilteredData = this.theData.filter(row =>
return row.tStartDate >= minDate && row.tEndDate <= maxDate);
答案 1 :(得分:0)
您可以使用在单击提交时切换的布尔值来切换已过滤的for循环:
* HTML:
<div>
<label>Start Date:</label>
<input type="text" [(ngModel)]="startDateValue">
</div>
<label>End Date:</label>
<input type="text" [(ngModel)]="endDateValue">
</div>
<tr *ngIf="filterToggle" *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>
<tr *ngIf="!filterToggle" *ngFor="let dPoint of theData; 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>
//the button that will execute the filtering
<button type="submit" class="btn icon-search" [disabled]="!secondForm.valid" (click)="submit()"></button>
</div>
* TS:
submit() {
this.filterToggle = !this.filterToggle;
}
不保留模板代码,但它应该可以工作。
另一个想法是发送一个布尔值“filterToggle”&#39;通过管道,所以管道本身不会过滤,除非过滤器切换&#39;是真的。因此,如果单击“提交”,则切换应更改为true,管道将进行过滤。
所以你的* ngFor会是这样的:
<tr *ngFor="let dPoint of theData | searchDates:startDateValue:endDate:filterToggle; let idx = index; let even=even">
...
</tr>
或者,您可以过滤实际数据,而不是通过管道。
submit(){
this.theData = this.theData.filter(data =>
return data.tStartDate >= this.startDateValue && data.tEndDate <= this.endDateValue);
}
因此,当您单击“提交”时,通过过滤来更改原始的theData数组。您可能必须使用布尔表达式来确定要过滤的内容。它应该只保留&#39;数据&#39;开始日期大于或等于您的&#39; this.startDateValue&#39;并且小于或等于你的&#39; this.endDateValue&#39;。但是,这将覆盖原始的theData数组。所以我会创建一个临时数组,以便您可以恢复到未经过滤的数据。