My data is stored in a JSON
file named tasks
. The structure of this template can be visualized as follows:
https://i.sstatic.net/MCSit.png
Data Structure of JSON File
[
{
"taskName": "Task - 1",
"id": "01",
"startDate": "2019-04-17T18:30:00Z" <==========
},
{
"taskName": "Task - 2",
"id": "02",
"startDate": "2019-04-22T14:30:00Z"
},
{
"taskName": "Task - 3",
"id": "03",
"startDate": "2019-04-17T12:30:00Z" <========
},
{
"taskName": "Task - 4",
"id": "04",
"startDate": "2019-04-25T18:10:00Z"
}
]
In the JSON file, there is a property called startDate
, based on which I aim to display relevant tasks like this:
https://i.sstatic.net/0jHIh.png
I attempted filtering by date with the following code snippets:
HTML Implementation
<div class="cust-detail" *ngFor="let task of getTaskByDate(myDate)">
<tr>
<td>Task Name: </td>
<td>{{task.taskName }}</td>
</tr>
</div>
Typescript Implementation
tasks: ITasks;
myDate = new Date();
constructor(private myService: MyService) {}
ngOnInit() {
this.myService.getTasks()
.subscribe(res => this.tasks = res);
}
public getTaskByDate (myDate: string): ITasks {
return this.tasks ? this.tasks.filter(x => x.startDate === myDate) : [];
}
If you'd like to explore a live demo showcasing this functionality, check it out here