如何修复角度日期选择器过滤器

时间:2019-08-29 12:53:10

标签: javascript angular

我正在尝试过滤FROM和TO将显示数据的日期,但是当FROM和TO相同时,它不会显示当前选择的日期。示例从:08-01-2019 00:00:00到:08-29-2019 13:00:00 它不会显示数据2019-08-28 12:36:01,但是当我尝试设置FROM:06-14-2019 00:00:00 TO:08-29-2019 13:00:00时显示所有数据

``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"
        }
    ]
}
``

2 个答案:

答案 0 :(得分:2)

您可以使用for简化您的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);
  });  
}

答案 1 :(得分:0)

  

已解决

之前您说的是 从:08-29-2019 00:00:00到:08-29-2019 13:00:00它不显示数据2019-08-28 12 :36:01 是的。因为 2019-08-28 12:36:01 不在这些范围内。

现在,由于您已经更新了问题,如果要过滤提到的日期,请按照以下简单的JavaScript解决方案进行操作,即可将其转换为有角的。

var data=[
    {
        "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"
    }
]
data.filter(function(x){ 
                    if(new Date(x.dateandtime)> new Date('06-12-2019 00:00:00') 
                    && new Date(x.dateandtime)< new Date('08-28-2019 14:00:00')){ return x }
      })