Packages
flop_phoenix
0.6.1
0.26.1
0.26.0
0.25.3
0.25.2
0.25.1
0.25.0
0.24.1
0.24.0
0.23.1
0.23.0
0.22.10
0.22.9
0.22.8
0.22.7
0.22.6
0.22.5
0.22.4
0.22.3
0.22.2
0.22.1
0.22.0
0.21.2
0.21.1
0.21.0
0.20.0
0.19.1
0.19.0
0.18.2
0.18.1
0.18.0
0.17.2
0.17.1
0.17.0
0.16.0
0.15.2
0.15.1
0.15.0
0.14.2
0.14.1
0.14.0
0.13.0
0.12.0
0.11.1
0.11.0
0.10.0
0.9.1
0.9.0
0.8.1
0.8.0
0.7.0
0.6.1
0.6.0
0.5.1
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
Phoenix components for pagination, sortable tables and filter forms using Flop.
Current section
Files
Jump to
Current section
Files
lib/flop_phoenix/table.ex
defmodule Flop.Phoenix.Table do
@moduledoc false
use Phoenix.HTML
import Phoenix.LiveView.Helpers
def render(assigns) do
~L"""
<table<%= if @opts[:table_class] do %> class="<%= @opts[:table_class] %>"<% end %>>
<thead>
<tr>
<%= for header <- @headers do %>
<%= header(header, @meta, @path_helper, @path_helper_args, @opts) %>
<% end %>
</tr>
</thead>
<tbody>
<%= for item <- @items do %>
<tr>
<%= for column <- @row_func.(item, @opts) do %>
<td><%= column %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
"""
end
defp header(
{value, field},
%Flop.Meta{flop: flop},
path_helper,
path_helper_args,
opts
) do
assigns = %{
__changed__: nil,
field: field,
flop: flop,
opts: opts,
path_helper: path_helper,
path_helper_args: path_helper_args,
value: value
}
~L"""
<th>
<%= if is_sortable?(field, opts[:for]) do %>
<%= live_patch(@value,
to:
Flop.Phoenix.build_path(
@path_helper,
@path_helper_args,
Flop.push_order(@flop, @field)
)
)
%>
<span class="<%= @opts[:symbol_class] || "order-direction" %>">
<%= @flop |> current_direction(@field) |> render_arrow(@opts) %>
</span>
<% else %>
<%= @value %>
<% end %>
</th>
"""
end
defp header(value, _, _, _, _) do
assigns = %{__changed__: nil, value: value}
~L"""
<th><%= @value %></th>
"""
end
defp is_sortable?(_, nil), do: true
defp is_sortable?(field, module),
do: field in (module |> struct() |> Flop.Schema.sortable())
defp render_arrow(nil, _), do: ""
defp render_arrow(direction, opts) do
assigns = %{__changed__: nil, direction: direction, opts: opts}
~L"""
<span class="<%= @opts[:symbol_class] || "order-direction" %>"><%=
if @direction in [:asc, :asc_nulls_first, :asc_nulls_last] do
Keyword.get(@opts, :symbol_asc, "▴")
else
Keyword.get(@opts, :symbol_desc, "▾")
end
%></span>
"""
end
defp current_direction(%Flop{order_by: nil}, _), do: nil
defp current_direction(
%Flop{order_by: order_by, order_directions: directions},
field
) do
order_by
|> Enum.find_index(&(&1 == field))
|> get_order_direction(directions)
end
defp get_order_direction(nil, _), do: nil
defp get_order_direction(_, nil), do: :asc
defp get_order_direction(index, directions), do: Enum.at(directions, index)
end