我的代码中有这个:
<%= for empresa <- @empresas do %>
<%= render myProject.ComponentView, "smallPlacard.html",
smallPlacard_id: "1",
smallPlacard_class: "Company",
smallPlacard_mainText: company.name
%>
<% end %>
我希望 smallPlacard_id
会针对每个呈现的元素自动递增。
以 Phoenix/函数式方式执行此操作的最佳方法是什么?
最佳答案
您可以使用 Enum.with_index/2 :
<%= for {empresa, id} <- Enum.with_index(@empresas) do %>
<%= render myProject.ComponentView, "smallPlacard.html",
smallPlacard_id: id + 1,
smallPlacard_class: "Company",
smallPlacard_mainText: company.name
%>
<% end %>
我在这个例子中递增了 1,因为索引是基于 0 的。如果您像以前一样需要字符串,请改用 "#{id + 1}"
。
关于elixir - 如何在 <%= for %> View 助手中增加 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36225064/