From 2b6b90c359070f5ecff00dea7041acc1e4728d4d Mon Sep 17 00:00:00 2001 From: Chris Nelson Date: Sat, 29 Jun 2024 17:42:33 -0400 Subject: [PATCH] lil bit o cleanup --- README.md | 42 ++++++++++++++++++- mix.exs | 2 - .../lib/test_bed_web/live/posts_live/index.ex | 3 +- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6422ec2..533eb0d 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,47 @@ This is a sortable, paginated table component for Ash resources or queries - [X] Sort by attribute - [X] Sort by relation (using function passed to column) -- [ ] Pagination +- [X] Pagination ## Usage -See the test_bed folder for an example with tests +Here's an example taken from the test_bed: + +```elixir +defmodule TestBedWeb.PostsLive.Index do + use TestBedWeb, :live_view + + def render(assigns) do + ~H""" + <.live_component id="posts_table" limit={10} offset={0} sort={{"id", :asc}} module={AshTable.Table} query={TestBed.Blog.Post}> + <:col :let={post} label="Id" sort_key="id"><%= post.id %> + <:col :let={post} label="Title" sort_key="title"> + <%= post.title %> + + <:col :let={post} label="Author" apply_sort={&sort_by_author/2} sort_key="author.name"> + <%= if post.author, do: post.author.name %> + + + """ + end + + require Ash.Sort + + defp sort_by_author(query, direction) do + Ash.Query.sort(query, {Ash.Sort.expr_sort(author.name), direction}) + end + +end +``` + +In this case the `TestBed.Blog.Post` resource has a title, content, and belongs to Author which has a name. The table is paginated, and sortable by Title and Author name. Note use of the `apply_sort` being passed into the `:col`. This is needed for sorting by related properties due to how Ash works, or til I better understand it and find a simpler way :) + +## Running the test_bed example project + +``` +cd test_bed +mix deps.get +mix ash.setup +mix phx.server +``` diff --git a/mix.exs b/mix.exs index 46c31bf..e84be22 100644 --- a/mix.exs +++ b/mix.exs @@ -25,8 +25,6 @@ defmodule AshTable.MixProject do {:ash, "~> 3.0"}, {:ash_postgres, "~> 2.0"}, {:ash_phoenix, "~> 2.0"} - # {:dep_from_hexpm, "~> 0.3.0"}, - # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} ] end end diff --git a/test_bed/lib/test_bed_web/live/posts_live/index.ex b/test_bed/lib/test_bed_web/live/posts_live/index.ex index 40dd5df..7b4863f 100644 --- a/test_bed/lib/test_bed_web/live/posts_live/index.ex +++ b/test_bed/lib/test_bed_web/live/posts_live/index.ex @@ -3,9 +3,8 @@ defmodule TestBedWeb.PostsLive.Index do def render(assigns) do ~H""" -

hi

<.live_component id="posts_table" limit={10} offset={0} sort={{"id", :asc}} module={AshTable.Table} query={TestBed.Blog.Post}> - <:col :let={post} label="Id" sort_key="id"><%= post.id %> + <:col :let={post} label="Id"><%= post.id %> <:col :let={post} label="Title" sort_key="title"> <%= post.title %>