简体   繁体   English

每次日期更改为 Angular 时,如何过滤?

[英]How can I filter every time the date changes with Angular?

I want to update the data in the table every time the date changes.我想在每次日期更改时更新表中的数据。 I prepared a function like this, but it only filters once.我准备了一个这样的function,但是只过滤一次。 It is necessary to refresh the page to perform a filtering for the second time.第二次过滤需要刷新页面。 I tried to refresh the page with the window.location.reload() function, but this time the data comes before filtering.我尝试使用window.location.reload() function 刷新页面,但这次数据在过滤之前出现。

This is my input这是我的输入

<div class="col-xxl-3 col-sm-4">
    <input (change)="filterByDate()" id="date-range-input" class="form-control bg-light border-light" type="text" mwlFlatpickr  placeholder="Select date" mode="range">
</div>

And this is my function这是我的 function

filterByDate() {
    this.dateRange = document.getElementById('date-range-input') as HTMLInputElement
    let dateRangeString = this.dateRange.value
    let startDateString = dateRangeString.substring(0,10)
    let endDateString = dateRangeString.substring(13,24)
    this.startDateObj = new Date(startDateString);
    this.endDateObj = new Date(endDateString);
    console.log(this.startDateObj, this.endDateObj)
    this.clientResults = this.clientResults.filter(m => new Date(m.createddate) >= this.startDateObj && new Date(m.createddate) <= this.endDateObj)
    this.clientResults.sort((a, b) => new Date(b.createddate).getTime() - new Date(a.createddate).getTime());
    this.resultCount = this.clientResults.length
    this.approvedResults = this.clientResults.filter(item => item.boolresult == true).length
    this.rejectedResults = this.clientResults.filter(item => item.boolresult == false).length
    this.totalResultAmount = this.clientResults.filter(item => item.transactionamount).reduce((sum, current) => sum + current.transactionamount, 0);
    this.dateRange = document.getElementById('date-range-input')?.removeAttribute('value')
}

The main problem here is that the filterByDate() function only works with the first change event.这里的主要问题是filterByDate() function 仅适用于第一个更改事件。 But I want to run this function on every change event.但是我想在每个更改事件上运行这个 function。 How can I do that?我怎样才能做到这一点?

It looks you are using angularx-flatpickr .看起来您正在使用angularx-flatpickr mwlFlatpickr has an event flatpickrChange which emits on date change. mwlFlatpickr有一个事件flatpickrChange ,它在日期更改时发出。 Try using it.尝试使用它。

<input (flatpickrChange)="filterByDate()" id="date-range-input" class="form-control bg-light border-light" type="text" mwlFlatpickr  placeholder="Select date" mode="range">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM