A flexible DataTable component for LiveView.
Source code for the screenshot above. You get all of this in ~50loc.
Some of the features the component has:
DataTable.Source
behaviour, usable with custom data sourcesSource
def render(assigns) do
~H"""
<DataTable.live_data_table
id="table"
source={{DataTable.Ecto, {MyApp.Repo, @source_query}}}>
<:col :let={row} name="Id" fields={[:id]} sort_field={:id}>
<%= row.id %>
</:col>
<:col :let={row} name="Name" fields={[:first_name, :last_name]}>
<%= row.first_name <> " " <> row.last_name %>
</:col>
</DataTable.live_data_table>
"""
end
def mount(_params, _session, socket) do
query = DataTable.Ecto.Query.from(
user in MyApp.User,
fields: %{
id: user.id,
first_name: user.first_name,
last_name: user.last_name
},
key: :id
)
socket = assign(socket, :source_query, query)
[...]
end
First you need to add data_table
to your mix.exs
:
defp deps do
[
{:data_table, "~> 1.0"}
]
end
If you want to use the default Tailwind
theme, you need to set up tailwind
to include styles
from the data_table
dependency.
Add this to the content
list in your assets/tailwind.js
:
"../deps/data_table/**/*.*ex"