英文:
How can I sort the data coming from the Service with Angular according to time and transfer it into a model?
问题
这是我在联系服务时接收到的数据。我想根据时间对这些数据进行排序,将其导入模型,然后在表格中向用户展示。我应该如何在Angular中完成这个任务?
这是我为接收到的数据设置的模型文件。
export interface ResultsModel {
"result": string,
"createddate": string,
"clientid": string,
"id": number,
"clientusername": string,
}
还有我的API请求函数。
getAllResults() {
let apiEndpoint = "results";
this.httpRequestService.getApi(apiEndpoint, false).subscribe(resultRequest => {
console.log(resultRequest)
})
}
英文:
This is the data I received in the response when I contacted the service. I want to sort this data according to time, import it into a model, and then show it to users in a table. How can I do this with Angular?
[
{
"id": 1,
"result": "string",
"ownername": "string",
"clientid": "string",
"createddate": "2023-01-07T15:44:50.408568",
"clientusername": "string",
},
{
"id": 2,
"result": "string",
"ownername": "string",
"clientid": "string",
"createddate": "2023-01-07T15:44:50.408568",
"clientusername": "string",
},
{
"id": 3,
"result": "string",
"ownername": "string",
"clientid": "string",
"createddate": "2023-01-07T15:44:50.408568",
"clientusername": "string",
},
{
"id": 4,
"result": "string",
"ownername": "string",
"clientid": "string",
"createddate": "2023-01-07T15:44:50.408568",
"clientusername": "string",
}
]
This is the model file I set up for the incoming data.
export interface ResultsModel {
"result": string,
"createddate": string,
"clientid": string,
"id": number,
"clientusername": string,
}
And this is my API Request function
getAllResults() {
let apiEndpoint = "results"
this.httpRequestService.getApi(apiEndpoint, false).subscribe(resultRequest => {
console.log(resultRequest)
})
答案1
得分: 0
首先,您可以通过解析响应并执行类似以下操作来对其进行排序:
response.sort((a, b) => {
return new Date(a.createddate).getTime() - new Date(b.createddate).getTime()
})
然后,您可以将数据插入到表格中,可以像这样:
<table>
<thead>
<tr>Id</tr>
...
..
.
</thead>
<tbody>
<tr *ngFor="let post of response">
<td>{{ post.id }}</td>
<td>{{ post.result }}</td>
...
..
.
</tr>
</tbody>
</table>
或者使用 Angular Material Table 组件(https://material.angular.io/components/table/overview)。
英文:
Firstly, you can sort the response by parsing it and then doing something like this:
response.sort((a, b) => {
return new Date(a.createddate).getTime() - new Date(b.createddate).getTime()
})
Then you can just plug the data into a table either like this:
<table>
<thead>
<tr>Id</tr>
...
..
.
</thead>
<tbody>
<tr *ngFor="let post of response">
<td>{{ post.id }}</td>
<td>{{ post.result }}</td>
...
..
.
</tr>
</tbody>
</table>
Or use the angular material Table component (https://material.angular.io/components/table/overview)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论