我如何使用Angular将从服务传来的数据按时间排序并转换为模型?

huangapple go评论118阅读模式
英文:

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) =&gt; {
  return new Date(a.createddate).getTime() - new Date(b.createddate).getTime()
})

Then you can just plug the data into a table either like this:

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;Id&lt;/tr&gt;
    ...
    ..
    .
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr *ngFor=&quot;let post of response&quot;&gt;
      &lt;td&gt;{{ post.id }}&lt;/td&gt;
      &lt;td&gt;{{ post.result }}&lt;/td&gt;
      ...
      ..
      .
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

Or use the angular material Table component (https://material.angular.io/components/table/overview)

huangapple
  • 本文由 发表于 2023年1月9日 05:49:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定