我目前正在为任何带有搜索字段的表创建部分模板。我需要生成从控制器传递的函数名,并在模板中使用选项作为链接。
我已经尝试了下面粘贴为code
的解决方案。它不起作用,我也不知道为什么。
控制器:
@table_links [{:show, []}, {:edit, []}, {:delete, [method: :delete, data: [confirmation: "Are you sure?"]]}]
此模块属性被分配给conn
并传递给模板。
渲染部分:
<%= render BaseAppWeb.SharedView, "table_partial.html",
Map.merge(assigns,
%{action: Routes.admin_users_path(@conn, :index),
opts: [method: :get],
columns: @searchable_columns,
table_links: @table_links,
links_path: &Routes.admin_users_path/3}) %>
部分链接生成:
<%= for {function, options} <- @table_links do %>
<%= case function do
:show -> {:safe, "<i class = "mdi mdi-magnify">"}
:edit -> {:safe, "<i class = "mdi mdi-pencil">"}
:delete -> {:safe, "<i class = "mdi mdi mdi-trash-can-outline">"}
_ -> {:safe, "<i class = " mdi mdi-arrow-right-bold-circle-outline">"}
end%>
<%= link(Atom.to_string(function) |> String.capitalize(), to: @links_path.(@conn, function, entity), options) %>
<% end %>
显示的错误:
lib/base_app_web/templates/shared/table_partial.html.eex:42:之前的语法错误:选项
如果不包括options
,如下所示,一切都很好,但我没有method:
作为链接选项,这在我的情况下是必要的。
<%= link(Atom.to_string(function) |> String.capitalize(), to: @links_path.(@conn, function, entity)) %>
我将非常感谢任何能帮助我将链接选项从控制器传递到模板中的链接的建议!
编辑:这是你要的table_partial.html.eex
:
<div class = "col-12">
<%= form_for @conn, @action, @opts, fn f -> %>
<%= Enum.reduce @columns, [], fn {function, {key, value}}, acc -> %>
<%= case function do
:date_input -> [acc] ++ [
build_form(f, {:label, {:from_date, "#{value} from"}}),
build_form(f, {function, {:from_date, value}}),
build_form(f, {:label, {:from_date, "#{value} to"}}),
build_form(f, {function, {:to_date, value}})]
_other -> [acc] ++ [build_form(f, {:label, {key, value}}), build_form(f, {function, {key, value}})]
end %>
<% end %>
<%= submit "Search", name: "order_by", value: "" %>
<hr>
<div class = "row">
<div class = "col-12">
<table class = "table dt-responsive nowrap talbe-borderless table-hover">
<thead class = "thead-light">
<tr>
<%= for {_function, {_key, value}} <- @searchable_columns do %>
<th><%= submit "#{value}", name: "order_by", value: "#{value}" %> </th>
<% end %>
<th>Options</th>
</tr>
</thead>
<% end %>
<tbody>
<%= for entity <- @entities do %>
<tr>
<%= for {_function, {key, _value}} <- @searchable_columns do %>
<td><%= Map.get(entity, key) %></td>
<% end %>
<td>
<%= for {function, options} <- @table_links do %>
<%= case function do
:show -> {:safe, "<i class = "mdi mdi-magnify">"}
:edit -> {:safe, "<i class = "mdi mdi-pencil">"}
:delete -> {:safe, "<i class = "mdi mdi mdi-trash-can-outline">"}
_ -> {:safe, "<i class = " mdi mdi-arrow-right-bold-circle-outline">"}
end%>
<%= link(Atom.to_string(function) |> String.capitalize(), to: @links_path.(@conn, function, entity), options) %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
我无法正常构建它们,因为我正在用这个模板构建多个不同的表,不同的表Options
可能会有所不同。
好吧,如果有人将来会寻找这样的东西——这是我刚刚找到的传递参数的解决方案:
控制器:
@table_links [{:show, %{:method => :get, :data => []}}, {:edit, %{:method => :get, :data => []}}, {:delete, %{:method => :delete, :data => [confirm: "Are you sure?"]}}]
table_partial.html.eex
<td>
<%= for {function, %{:method => method, :data => data}} <- @table_links do %>
<%= case function do
:show -> {:safe, "<i class = "mdi mdi-magnify">"}
:edit -> {:safe, "<i class = "mdi mdi-pencil">"}
:delete -> {:safe, "<i class = "mdi mdi mdi-trash-can-outline">"}
_ -> {:safe, "<i class = " mdi mdi-arrow-right-bold-circle-outline">"}
end%>
<%= link(Atom.to_string(function) |> String.capitalize(), to: @links_path.(@conn, function, entity), method: method, data: data) %>
<% end %>
</td>
这很容易,但是确认仍然不起作用。这是因为它是confirm:
,而不是confirmation:
——现在运行良好:)
问题是您试图将options
传递到关键字列表中。您将希望以两种方式之一合并列表。你可以做
Keyword.merge([to: @links_path.(@conn, function, entity)], options)
或
[to: @links_path.(@conn, function, entity)] ++ options
在中
<%= link(Atom.to_string(function) |> String.capitalize(), to: @links_path.(@conn, function, entity), options) %>