Чтобы достичь ожидаемого результата, вам нужно изменить тип dataSource
.Также вам нужен метод для перестройки массива элементов на основе выбора диапазона дат пользователя.
Ваш xxx.component.ts
должен выглядеть следующим образом.
import { Component } from '@angular/core';
import { MatTableDataSource } from '@angular/material';
import { DatePipe } from '@angular/common';
import {FormControl, FormGroup} from '@angular/forms';
import * as moment from 'moment';
export interface PeriodicElement {
name: string;
position: number;
weight: number;
DOB: Date;
created: Date;
}
const ELEMENT_DATA: PeriodicElement[] = [
{ position: 1, name: 'Hydrogen', weight: 1.0079, DOB: new Date(2016, 11, 24), created: new Date(2015, 15, 24) },
{ position: 2, name: 'Helium', weight: 4.0026, DOB: new Date(2018, 18, 24), created: new Date(2018, 11, 24) },
{ position: 3, name: 'Lithium', weight: 6.941, DOB: new Date(1993, 6, 12), created: new Date(1999, 12, 15) },
{ position: 4, name: 'Beryllium', weight: 9.0122, DOB: new Date(2001, 7, 6), created: new Date(2011, 10, 6) },
{ position: 5, name: 'Boron', weight: 10.811, DOB: new Date(2020, 5, 9), created: new Date(2020, 5, 9) },
{ position: 6, name: 'Carbon', weight: 12.0107, DOB: new Date(2008, 7, 14), created: new Date(2008, 7, 14) },
{ position: 7, name: 'Nitrogen', weight: 14.0067, DOB: new Date(1998, 11, 18), created: new Date(1998, 11, 18) },
{ position: 8, name: 'Oxygen', weight: 15.9994, DOB: new Date(2002, 2, 24), created: new Date(2002, 2, 24) },
{ position: 9, name: 'Fluorine', weight: 18.9984, DOB: new Date(2006, 4, 29), created: new Date(2006, 4, 29) },
{ position: 10, name: 'Neon', weight: 20.1797, DOB: new Date(2040, 5, 30), created: new Date(2040, 5, 30) },
];
/**
* @title Table with filtering
*/
@Component({
selector: 'table-filtering-example',
styleUrls: ['table-filtering-example.css'],
templateUrl: 'table-filtering-example.html',
})
export class TableFilteringExample {
displayedColumns: string[] = ['position', 'name', 'weight', 'DOB', 'founded'];
dataSource = ELEMENT_DATA;
pipe: DatePipe;
filterForm = new FormGroup({
fromDate: new FormControl(),
toDate: new FormControl(),
});
get fromDate() { return this.filterForm.get('fromDate'); }
get toDate() { return this.filterForm.get('toDate'); }
constructor() {
}
getDateRange(value) {
// getting date from calendar
const fromDate = value.fromDate
const toDate = value.toDate
const tempData = <any>this.dataSource;
let selectedItems: PeriodicElement[] = [];
if(fromDate !== '' && toDate !== '') {
tempData.forEach((item, index) => {
if (item.DOB >= fromDate && item.DOB <= toDate) {
selectedItems.push(item);
}
});
this.dataSource = selectedItems;
}
}
applyFilter(filterValue: string) {
// this.dataSource.filter = filterValue.trim().toLowerCase();
}
}