elixir - 如何在 <%= for %> View 助手中增加 ID

标签 elixir phoenix-framework

我的代码中有这个:

<%= 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/

相关文章:

iis - 在 Windows 上的 IIS 中运行 Phoenix 时内存泄漏/CPU 使用率持续居高不下

elixir - 我可以在 Ecto 中使用其中一个关联的聚合计数来查询模型吗?

logging - Phoenix 删除 [调试] 日志记录

elixir - 无法删除模型——函数 Ecto.Query.__changeset__/0 未定义或私有(private)

struct - Plug.Conn.Unfetched 未实现 Access 行为

compiler-errors - Windows 上的 Addict 十六进制包依赖项错误在 Mac 上不会发生

elixir - Phoenix 和 Ecto 以及 SELECT

testing - Elixir - 在 doctest 中使用变量

oauth-2.0 - 如何要求登录 Elixir Phoenix 应用程序?

elixir - 将数据从一种形式转换为另一种形式