【发布时间】:2019-08-07 18:39:32
【问题描述】:
我目前正在为任何带有搜索字段的表格制作部分模板。我需要生成从控制器函数名称传递的选项作为模板中的链接。
我已经尝试过在下面粘贴为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_partial.html.eex 文件吗?我无法重现您的问题。另外,你为什么用你的链接这样做?为什么不直接构建链接而不是遍历列表?
-
是的,当然 - 我已将其粘贴到
Edit。我还写了一个说明——我无法正常构建它们,因为我正在使用这个模板构建多个不同的表,并且不同的表选项可能会有所不同。
标签: html elixir phoenix-framework