Skip to content

Commit

Permalink
lil bit o cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Jun 29, 2024
1 parent 97dc446 commit 2b6b90c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
<:col :let={post} label="Title" sort_key="title">
<%= post.title %>
</:col>
<:col :let={post} label="Author" apply_sort={&sort_by_author/2} sort_key="author.name">
<%= if post.author, do: post.author.name %>
</:col>
</.live_component>
"""
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
```

2 changes: 0 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions test_bed/lib/test_bed_web/live/posts_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ defmodule TestBedWeb.PostsLive.Index do

def render(assigns) do
~H"""
<h1>hi</h1>
<.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>
<:col :let={post} label="Id"><%= post.id %></:col>
<:col :let={post} label="Title" sort_key="title">
<%= post.title %>
</:col>
Expand Down

0 comments on commit 2b6b90c

Please sign in to comment.