rendering a list of structs into phoenix template

hope everyone is doing great! i am new here and i have this list of structs rendered on my liveview

[%Apollo.Accounts.Schema.ImportedEmailStatu{
    id: 14,
    imported_email_id: 37,
    inserted_at: ~N[2019-06-06 07:17:11],
    job_id: 1,
    status: "matched",
    updated_at: ~N[2019-06-06 07:17:11],
    children: []
  },
    %Apollo.Accounts.Schema.ImportedEmailStatu{
    id: 15,
    imported_email_id: 37,
    inserted_at: ~N[2019-06-06 07:17:11],
    job_id: 1,
    status: "matched",
    updated_at: ~N[2019-06-06 07:17:11],
    children: []
  },
  %Apollo.Accounts.Schema.ImportedEmailStatu{
    id: 16,
    imported_email_id: 37,
    inserted_at: ~N[2019-06-06 07:17:11],
    job_id: 1,
    status: "unmatched",
    updated_at: ~N[2019-06-06 07:17:11],
    children: []
  }]

i want to render this in a nice way. I tried turning this into a map like this, which in my opinion not the best way to do it(i don’t want to manually rewrite all the attributes):

list =
  # Tesla http client
  #     client
  #     |> API.get_accounts()
  #     |> Enum.map(& &1.id)
  #   list |> Enum.group_by(
  #     & &1.id,
  #     &%{
  #       "id" => &1.id,
  #       "status" => &1.status,
  #       "updated_at" => &1.updated_at,
  #       "due_at" => &1.due_at,
  #       "children" => &1.children
  #     }
  #   )

but i get this error: 1st argument: not an atom

TLDR: i want to render list of structs into an HTML table

thank you for your reply in advance .

Comprehension list is what I’d use:

for struct <- list_of_structs do
<p>struct.id</p>
<p>struct.status</p>
...
end

I’m on mobile so sorry about the small snippet, but you should be able to expand on this idea.

2 Likes

If you can forgive the Surface syntax, I allow the user to change the column selection and do it like so:

<table>
  <thead>
    <tr>
      {#for {field, i} <- Enum.with_index(@selected_columns)}
        <th>
          {Items.field_label(field)}
        </th>
      {/for}
    </tr>
  </thead>
  <tbody>
    {#for {row, i} <- Enum.with_index(@items)}
      <tr>
        {#for {field, j} <- Enum.with_index(@selected_columns)}
          <td>
            {#case field}
              {#match t when t in [:timefield, :timefield2]}
                {Items.format_datetime(row[field], @timezone)}
              {#match _}
                {row[field]}
            {/case}
          </td>
        {/for}
      </tr>
    {/for}
  </tbody>
</table>
2 Likes