I want to display a list of nested maps
The list is like this though there is actually several layers of nesting:
list = [%{no: 12345, name: "test1" , types: %{type_no: 1, type_name: "test_type}}, %{no: 12346, name: "test2" , types: %{type_no: 2, type_name: "test_type2}}
And I’m trying to represent it in the template with something like this
<%= for list ← @list do %>
<%= list.no %>
<%= list.name %>
<%= for list.type ← @list.types do %>
<%= list.type_no %>
<%= list.type_name %>
<% end %>
<% end %>
But I just get an error whatever I try, something like - cannot invoke remote function
Is there a way to do this? Or do I need to split the list before rendering it?
Any help would be appreciated
Thanks
Any time you ask a question about an error, please always include a copy / paste of the exact error. It’s the best way for us to help.
This line here <%= for list.type ← @list.types do %>
looks wrong, you probably want to be using the variable that you’ve bound: <%= for list.type ← list.types do %>
1 Like
I am trying to find out if there is a way to render a nested list of maps
I haven’t bound the nested list to any variable - I don’t know how I would do that?
You have @list
which is a list of maps. When you did <%= for list ← @list do %>
you bound each item of the list into a list
variable, which does make it sort of confusing. It’d be better if you called that <%= for item ← @list do %>
.
Sorry I don’t understand - how do I get the nested map to display?
I have bound the list to a variable but each item in the list has a list of several items (item types)
I don’t have the item types bound to a variable.
I want to display each item and, underneath each item, display the list of item types
How to I get the <%= for item ← @list do %> to work
When I try <%= for list.item ← @list do %>
I get
cannot invoke remote function list.item/0 inside a match
I don’t know how to access the nested list.
Try this:
<%= for item <- @list do %>
<%= list.no %>
<%= list.name %>
<%= for type <- list.types do %>
<%= type.type_no %>
<%= type.type_name %>
<% end %>
<% end %>
1 Like