Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix angular datepicker filter

I'm trying to filter the date which where the FROM and TO will display the data,

but when the FROM and TO are the same it doesn't display the current date selected.

Example FROM: 08-01-2019 00:00:00 | To: 08-29-2019 13:00:00 it doesn't display the data 2019-08-28 12:36:01, but when I try to set the FROM: 06-14-2019 00:00:00 | TO: 08-29-2019 13:00:00 it displays all the data

templog.component.ts

columnDefs: any = [
    { headerName: 'Date Time', field: 'dateandtime' },
    { headerName: 'Location', field: 'sensor' },
    { headerName: 'Temperature', field: 'temperature' },
    { headerName: 'Humidity', field: 'humidity' }
  ];

submitForm(): void {
    this.rowData = record.default.records;

    const dateStart = format(this.validateForm.value.datePickerStart, 'YYYY-MM-DDTHH:mm:ss');
    const dateEnd = format(this.validateForm.value.datePickerEnd, 'YYYY-MM-DDTHH:mm:ss');
    
    for(let i = 0;i < this.rowData.length; i++){
      if (isBefore(this.rowData[i].dateandtime, dateStart) || 
        isAfter(this.rowData[i].dateandtime, dateEnd )) {  
          this.rowData = this.rowData.splice(i, 0);
      }
    }    
  }

templog.json

{
    "records": [
        {
            "dateandtime": "2018-06-14 01:38:02",
            "sensor": "Sewing Line1",
            "temperature": "25.8",
            "humidity": "99.9"
        },
        {
            "dateandtime": "2018-06-14 01:36:01",
            "sensor": "Sewing Line1",
            "temperature": "25.8",
            "humidity": "99.9"
        },
        {
            "dateandtime": "2018-06-14 01:36:01",
            "sensor": "Heat Seal Area",
            "temperature": "25.9",
            "humidity": "99.9"
        },
        {
            "dateandtime": "2019-08-28 12:36:01",
            "sensor": "Heat Seal Area",
            "temperature": "25.9",
            "humidity": "99.9"
        }
    ]
}
like image 974
ABC Avatar asked Aug 11 '26 23:08

ABC


1 Answers

Your for loops can be simplified with a filter

export interface MyRecord {
    dateandtime: string,
    sensor: string,
    temperature: string,
    humidity: string
}

public rowData: MyRecord[] = [];

submitForm(): void {
  this.rowData = record.default.records;

  const dateStart = format(this.validateForm.value.datePickerStart, 'YYYY-MM-DDTHH:mm:ss');
  const dateEnd = format(this.validateForm.value.datePickerEnd, 'YYYY-MM-DDTHH:mm:ss');

  this.rowData = this.rowData.filter((data: MyRecord) => {
    // keep data which are not out of date-bound
    return !isBefore(data.dateandtime, dateStart) && !isAfter(data.dateandtime, dateEnd);
  });  
}
like image 83
Random Avatar answered Aug 13 '26 11:08

Random



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!