我在elixir中有一个结构列表,我想循环遍历它们以显示每个结构的值:
[%InvoiceApplication.Billing.LineItem{__meta__: #Ecto.Schema.Metadata<:loaded, "billing_line_items">,
currency_type: 1, currency_value: 1.0e3, description: "sdadsaasfasf", id: 1,
inserted_at: ~N[2017-03-14 13:56:12.982080],
invoice: #Ecto.Association.NotLoaded<association :invoice is not loaded>,
invoice_id: 1, item: "item1", tax: 10,
updated_at: ~N[2017-03-14 13:56:12.982095]},
%InvoiceApplication.Billing.LineItem{__meta__: #Ecto.Schema.Metadata<:loaded, "billing_line_items">,
currency_type: 2, currency_value: 2.0e3, description: "dfadf asdfadsf", id: 2,
inserted_at: ~N[2017-03-14 13:56:13.110123],
invoice: #Ecto.Association.NotLoaded<association :invoice is not loaded>,
invoice_id: 1, item: "item 2", tax: 20,
updated_at: ~N[2017-03-14 13:56:13.110136]},
%InvoiceApplication.Billing.LineItem{__meta__: #Ecto.Schema.Metadata<:loaded, "billing_line_items">,
currency_type: 3, currency_value: 3.0e3, description: "adsfaf asfadf", id: 3,
inserted_at: ~N[2017-03-14 13:56:13.111817],
invoice: #Ecto.Association.NotLoaded<association :invoice is not loaded>,
invoice_id: 1, item: "item 3", tax: 30,
updated_at: ~N[2017-03-14 13:56:13.111829]}]
我需要显示
<td> Item 1 </td>
<td> Item 2 </td>
<td> Item 3 </td>
答案 0 :(得分:1)
您可以使用Enum.map
或for
来执行此操作:
<%= for item <- @items do %>
<td><%= item.item %></td>
<% end %>
或
<%= Enum.map(@items, fn(item) -> %>
<td><%= item.item %></td>
<% end) %>