角度材料表日期输入范围过滤

时间:2021-01-16 13:14:15

标签: html angular angular-material

我正在尝试根据材料日期选择器的 2 个输入字段过滤材料表。表数据源来自 table.component.ts 构造函数上的注入服务。但我不能让它工作。

table.component.html 代码:

<div>
    <form [formGroup]="form" (submit)="applyDateFilter()">
    <mat-form-field>
        <input 
        matInput 
        placeholder="Date From" 
        [matDatepicker]="picker1"
        formControlName="fromDate"
        required
        (click)="picker1.open()"
        >
        <mat-datepicker-toggle 
        matSuffix [for]="picker1"
        ></mat-datepicker-toggle>
        <mat-datepicker 
        #picker1></mat-datepicker>
    </mat-form-field>
    <mat-form-field>
        <input 
        matInput 
        placeholder="toDate" 
        [matDatepicker]="picker2"
        formControlName="toDate"
        required
        (click)="picker2.open()"
        >
        <mat-datepicker-toggle 
        matSuffix [for]="picker2"
        ></mat-datepicker-toggle>
        <mat-datepicker 
        #picker2></mat-datepicker>
    </mat-form-field>
    <button 
    class="button-form" 
    mat-raised-button color="accent" 
    (click)="applyDateFilter()"
    [disabled]="form.invalid">Submit</button>
    <button mat-raised-button color="accent" type="button" [disabled]="form.invalid">Clear</button>
    </form>
    <mat-form-field>
        <input matInput type="text" #filter (keyup)="doFilter(filter.value)" placeholder="Filter">
    </mat-form-field>
</div>
<mat-table 
[dataSource]="dataSource" matSort  
matSortActive="date"
matSortDirection="desc"
>

<ng-container matColumnDef="id">
    <mat-header-cell *matHeaderCellDef  mat-sort-header>Id</mat-header-cell>
    <mat-cell *matCellDef="let element">{{element.id }}</mat-cell>
</ng-container>

<ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef  mat-sort-header>Name</mat-header-cell>
    <mat-cell *matCellDef="let element">{{element.name }}</mat-cell>
</ng-container>

<ng-container matColumnDef="description" >
    <mat-header-cell *matHeaderCellDef mat-sort-header>Description</mat-header-cell>
    <mat-cell *matCellDef="let element">{{element.description }}</mat-cell>
</ng-container>

<ng-container matColumnDef="email" >
    <mat-header-cell *matHeaderCellDef mat-sort-header>Email</mat-header-cell>
    <mat-cell *matCellDef="let element">{{ element.email }}</mat-cell>
</ng-container>

<ng-container matColumnDef="phone" >
    <mat-header-cell *matHeaderCellDef mat-sort-header>Phone</mat-header-cell>
    <mat-cell *matCellDef="let element">{{ element.phone }}</mat-cell>
</ng-container>

<ng-container matColumnDef="date">
    <mat-header-cell *matHeaderCellDef mat-sort-header matSortStart="desc">Date</mat-header-cell>
    <mat-cell *matCellDef="let element">{{element.date |  date: 'dd/MM/yyyy'}}</mat-cell>
</ng-container>

<ng-container matColumnDef="status" >
    <mat-header-cell *matHeaderCellDef mat-sort-header>Status</mat-header-cell>
    <mat-cell *matCellDef="let element">{{element.status }}</mat-cell>
</ng-container>

<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

table.component.ts 代码:

import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { Company } from '../model/company.model';
import { TableService } from '../service/table.service';

@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.css']
})
export class TableComponent implements OnInit, AfterViewInit {
  displayedColumns = [
    'id',
    'name',
    'description',
    'email',
    'phone',
    'date',
    'status'
  ];
  form = new FormGroup({
    fromDate: new FormControl(null, { validators: [Validators.required]}),
    toDate: new FormControl(null, { validators: [Validators.required]})
  });
  dataSource = new MatTableDataSource<Company>();
  @ViewChild(MatSort) sort: MatSort;

  constructor(private tableService: TableService) { }

  ngOnInit(): void {
    this.dataSource.data = this.tableService.getCompanies();
  }

  ngAfterViewInit() {
    this.dataSource.sort = this.sort;
  }

  applyDateFilter() {
    console.log(this.form);
    if (this.form.invalid) {
      return
    }
    //this.dataSource.data = this.dataSource.data.filter(e=>e.date >= this.fromDate && e.date <= this.toDate);
  }

  doFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }

}

table.service.ts 代码:

import { Subject } from "rxjs";
import { Company } from "../model/company.model";

export class TableService {
    companyChanged = new Subject<Company>();

    private companies: Company[] = [
        {id: '1', name: 'Company1', description: 'test1 test1', email: 'test1@test.com', phone: '9999-99-9', date: new Date(2020, 1, 16), status: 'active'},
        {id: '2', name: 'Company2', description: 'test2 test1', email: 'test2@test.com', phone: '9999-99-9', date: new Date(2020, 1, 21), status: 'active'},
        {id: '3', name: 'Company3', description: 'test3 test1', email: 'test3@test.com', phone: '9999-99-9', date: new Date(2020, 1, 22), status: 'active'},
        {id: '4', name: 'Company4', description: 'test4 test1', email: 'test4@test.com', phone: '9999-99-9', date: new Date(2020, 1, 23), status: 'inactive'}
    ];

    getCompanies() {
        return this.companies.slice();
    }
}

company.model.ts 代码:

    export interface Company {
    id: string;
    name: string;
    description: string;
    email: string;
    phone: string;
    date?: Date;
    status: 'active' | 'inactive' | null
}

这是该项目在 GitHub 上的链接: https://github.com/christianlacuesta/angular-material-table-date-range-filter

1 个答案:

答案 0 :(得分:1)

我的朋友帮助了我。

将此代码添加到 applyDateFilter() 方法中。

  applyDateFilter() {
    console.log(this.form);
    this.dataSource.data = this.tableService.getCompanies();
    this.dataSource.data = this.dataSource.data.filter(e=> e.date >= this.form.value.fromDate && e.date <= this.form.value.toDate);
  }

并更新 table.service.ts 上的日期

   private companies: Company[] = [
        {id: '1', name: 'Company1', description: 'test1 test1', email: 'test1@test.com', phone: '9999-99-9', date: new Date('01/16/2021'), status: 'active'},
        {id: '2', name: 'Company2', description: 'test2 test1', email: 'test2@test.com', phone: '9999-99-9', date: new Date('01/21/2021'), status: 'active'},
        {id: '3', name: 'Company3', description: 'test3 test1', email: 'test3@test.com', phone: '9999-99-9', date: new Date('01/22/2021'), status: 'active'},
        {id: '4', name: 'Company4', description: 'test4 test1', email: 'test4@test.com', phone: '9999-99-9', date: new Date('01/23/2021'), status: 'inactive'}
    ];