我正在尝试根据所选的日期范围来过滤我的表,并且当我按下“提交”按钮时,该表为空,并且控制台日志中没有过滤的数据。我尝试过几种不同的方法,但是没有运气。在输入dataSource.filter =''+ Math.random()之前,我还有一个有效的搜索过滤器。任何帮助将不胜感激!
TS
import { Component, OnInit, ViewChild } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { MatSort} from '@angular/material/sort';
import {MatPaginator} from '@angular/material/paginator';
import {MatTableDataSource} from '@angular/material/table';
import { MatDialog, MatDialogConfig } from '@angular/material';
import { DialogDataComponent } from "../dialog-data/dialog-data.component";
import { DatePipe } from '@angular/common';
import {FormControl, FormGroup} from '@angular/forms';
export interface User {
id: number;
name: string;
distributor: string;
storeNum: number;
poNum: number;
createDate: Date;
recDate: Date;
totalUnits: number;
}
@Component({
selector: 'app-order-table',
templateUrl: './order-table.component.html',
styleUrls: ['./order-table.component.scss']
})
export class OrderTableComponent implements OnInit{
[x: string]: any;
public users: User[];
private usersUrl = 'api/users';
displayedColumns: string[] = ['name', 'distributor', 'storeNum', 'poNum', 'createDate', 'recDate', 'totalUnits'];
pipe: DatePipe;
dataSource = new MatTableDataSource<User>(this.users);
@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
@ViewChild(MatSort, {static: true}) sort: MatSort;
filterForm = new FormGroup({
fromDate: new FormControl(),
toDate: new FormControl(),
});
get fromDate() { return this.filterForm.get('fromDate').value; }
get toDate() { return this.filterForm.get('toDate').value; }
constructor(private http: HttpClient, public dialog: MatDialog) {};
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
ngOnInit() {
this.getUsers().subscribe(data => this.dataSource.data = data)
this.dataSource.paginator = this.paginator;
console.log(this.dataSource);
}
getUsers(): Observable<User[]> {
return this.http.get<User[]>(this.usersUrl)
}
openDialog(): void {
const dialogRef = this.dialog.open(DialogDataComponent, new MatDialogConfig())
}
onNoClick(): void {
this.dialogRef.close();
}
applyFilter(search: string, start: Date, end: Date) {
this.dataSource.filter = search.trim().toLocaleLowerCase();
this.pipe = new DatePipe('en');
this.dataSource.filterPredicate = (data, filter) =>{
if (start && end) {
//1. Need to determine why filtered data wont push to the table
return data.createDate >= start && data.createDate <= end;
}
return true;
}
//2. Need to determine what is being filtered/filter value
this.dataSource.filter= ''+Math.random();
}
}