【发布时间】:2021-04-08 05:31:41
【问题描述】:
这是我的html。我使用 for 循环在 html 中调用了 display[] 数组来显示数据。 我的目标是最初在页面加载时显示所有列表,我还想根据给定的日期输入过滤数据。
但是,到目前为止,当我根据日期过滤数据时。 示例:如果我选择从 2020 年 12 月 1 日到 2020 年 12 月 7 日的日期。 尽管 12 月 7 日有记录,但网格显示为空。 同样,如果我选择从 12 月 1 日到 12 月 15 日的日期,它会显示 12 月 15 日以下的所有记录,但不包括 15 日的数据。
我想显示所选 2 个日期之间的记录。 但截至目前,它显示的日期低于所选的第二个日期。
<input type="date" (change)="changeFirstInput($event)"> -
<input type="date" (change)="changeSecondInput($event)">
<table style="width:100%">
<thead>
<tr>
<th>name</th>
<th>date</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let o of display"> <!-- change made here only -->
<td>{{o.name}}</td>
<td>{{o.ts | date: 'dd/MM/yyyy'}}</td>
</tr>
</tbody>
</table>
我从服务中将数据检索到一个数组中:Info[]。 info 包含一个对象数组。 我还采用了一个名为 display[] 的新数组,它应该在过滤日期时动态变化。
pattern = /(\d{2})\.(\d{2})\.(\d{4})/;
public date1: Date = new Date("2000-01-01");
public date2: Date = new Date();
display = [];
changeFirstInput(e) {
this.date1 = new Date(e.target.value.replace(this.pattern, '$3-$2-$1'));
}
changeSecondInput(e) {
this.display = Object.assign([], this.Info);
this.date2 = new Date(e.target.value.replace(this.pattern, '$3-$2-$1'));
this.display = this.Info.filter(o =>
new Date(o.ts.replace(this.pattern, '$3-$2-$1')) >= this.date1 &&
new Date(o.ts.replace(this.pattern, '$3-$2-$1')) <= this.date2);
}
}
Info[] 中的数据。 我需要根据给定的日期输入过滤这些数据。
0: {
id: 1,
name: "Testing",
status: "Y",
ts: "2018-12-07T08:02:00.000Z"}
【问题讨论】:
-
请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?
标签: angular