Packages

Helpers built to work with Phoenix's page struct to easily work with CSV files.

Current section

Files

Jump to
ex_csv_editor lib csv_editor show.ex
Raw

lib/csv_editor/show.ex

defmodule CsvEditor.Show do
@moduledoc """
Documentation for CsvEditor.Show
"""
@doc """
CSV Editor Show Data
## Examples
iex> CsvEditor.Show.data([], [["cell 1", "cell 2", "cell 3"], ["cell 4", "cell 5", "cell 6"]], ["head 1", "head 2", "head 3"])
{:safe,
[60, "table", [[32, "class", 61, 34, "csv-editor", 34]], 62,
[[60, "thead", [], 62,
[60, "tr", [], 62,
[[60, "th", [], 62, "", 60, 47, "th", 62],
[[60, "th", [], 62, "head 1", 60, 47, "th", 62],
[60, "th", [], 62, "head 2", 60, 47, "th", 62],
[60, "th", [], 62, "head 3", 60, 47, "th", 62]]], 60, 47,
"tr", 62], 60, 47, "thead", 62],
[60, "tbody", [], 62,
[[60, "tr", [], 62,
[[60, "th", [], 62, "1", 60, 47, "th", 62],
[[60, "td", [], 62, "cell 1", 60, 47, "td", 62],
[60, "td", [], 62, "cell 2", 60, 47, "td", 62],
[60, "td", [], 62, "cell 3", 60, 47, "td", 62]]], 60, 47,
"tr", 62],
[60, "tr", [], 62,
[[60, "th", [], 62, "2", 60, 47, "th", 62],
[[60, "td", [], 62, "cell 4", 60, 47, "td", 62],
[60, "td", [], 62, "cell 5", 60, 47, "td", 62],
[60, "td", [], 62, "cell 6", 60, 47, "td", 62]]], 60, 47,
"tr", 62]], 60, 47, "tbody", 62]], 60, 47, "table", 62]}
"""
import Phoenix.HTML.Tag
def data(options, file) when is_bitstring(file) do
{header, body} = CsvEditor.decode(file)
data(options, body, header)
end
def data(options, body, header \\ []) do
content_tag(:table, content(body, header), [class: "csv-editor"])
end
defp content(body, header) do
[header(header), body(body)]
end
defp header(header) do
content_tag(:thead, content_tag(:tr, [cell("", :th), Enum.map(header, fn(e) -> cell(e, :th) end)]))
end
defp body(body) do
content_tag(:tbody, row(body, :td))
end
defp row(contents, tag) do
Enum.with_index(contents) |> Enum.map(fn{c, index} -> row(c, tag, index) end)
end
defp row(c, tag, index) when is_list(c) do
content_tag(:tr, [cell(index + 1, :th), Enum.map(c, fn(e) -> cell(e, tag) end)])
end
defp row(c, tag, _index) do
cell(c, tag)
end
defp cell(c, tag) do
content_tag(tag, c)
end
end