How to display data on web by using .tpl with data from nested json with template is .tpl

69 Views Asked by At

I want to display data using beego and golang. But I have a problem because I get is nested json data and I can't find a way to solve it, can you help me cause i'm new in beego and golang.

here's the nested json i got

[{
  "AppId":1233,
  "AppStatus":"approved",
  "AppStatusVerification":"retur",
  "AppFullname":"Dadang Subur",
  "UpdatedAt":"2022-03-31T11:54:07.283Z",
  "CreatedAt":"2021-03-23T06:20:07.877Z",
  "History":[
     {
        "Id":1233,
        "Name":"approved",
        "Time":"2022-03-28T17:31:24.55Z",
        "ActorId":1232,
        "ActorName":""
     },
     {
        "Id":1233,
        "Name":"approved",
        "Time":"2022-03-28T16:33:29.033Z",
        "ActorId":1232,
        "ActorName":""
     }}]

And code .tpl i used

<tbody>
    {{range $record := .records}}
       <tr>
         <td>{{$record.AppId}}</td>
         <td>{{$record.AppFullname}}</td>
         <td>{{$record.AppStatus}}</td>
         <td>{{$record.CreatedAt}}</td>
         <td>{{$record.UpdatedAt}}</td>
        </tr>
     {{end}}
</tbody>

Cause when i used <td>{{$record.Name}}</td> to get "approved", the result is executing "eform/listAllActivityHistoryAgent.tpl" at <$record.Name>: can't evaluate field Name in type models.HistoryData

Thank you in advance

1

There are 1 best solutions below

0
Drizzt On

History is also an array so you need to iterate in the tpl.

Please try this;

<tbody>
    {{range $record := .records}}
       <tr>
         <td>{{$record.AppId}}</td>
         <td>{{$record.AppFullname}}</td>
         <td>{{$record.AppStatus}}</td>
         <td>{{$record.CreatedAt}}</td>
         <td>{{$record.UpdatedAt}}</td>
         {{range $history := $record.History}}
         <td>{{$history.Name}}</td>
         {{end}}
        </tr>
     {{end}}
</tbody>