我有一個表,我成功使用自定義管道過濾。該過濾器基於兩個輸入,它們一起形成一個表格。我想添加的功能是在單擊提交按鈕之前不會發生過濾。所以它更像是一個搜索按鈕。我發現很多關於管道的信息,但沒有關於按鈕點擊時激活它們的信息。角度過濾器表使用自定義管道按鈕單擊
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;
});
}
}
更新您的帖子與** mainpage.component.html ** – Aravind
@Aravind我試圖包括最小代碼,就足以讓問題得到理解。 ** mainpage.component.html **的哪些部分會讓你感到困惑?或者你覺得缺少什麼? – Drew13
因爲您的問題全部是**按鈕點擊**和自定義管道。觸發點擊事件的按鈕在哪裏? – Aravind