superchris

superchris

As part of my experiments in learning Ash, I decided to extract a sortable, paginated table component we use on one of our projects and rework it to use Ash. It turned out to be not too terrible, so I thought I’d [share it with the world. It’s very early, and has almost no styling. But it works, has tests, and if nothing else might serve as a useful example for folks to build on.

As with all my work, feedback and contributions are welcomed :slight_smile:

https://github.com/launchscout/ash_table

First 10 of 12 Posts Switch mode

zachdaniel

zachdaniel

Creator of Ash

Awesome :slight_smile:

zachdaniel

zachdaniel

Creator of Ash

Some small notes:

Additional action opts

You’ll likely want to accept additional options to the action for things like authorization, multitenancy, etc. A name used for this in some places is action_opts or read_opts.

results =
      query |> apply_sort(sort, columns) |> Ash.read!(Keyword.merge(assigns[:action_opts] || [], [page: [limit: limit, offset: offset]]))

Multiple pagination strategies

There is keyset and offset pagination available in core, for keyset pagination, you’d replace offset with an after or before key. With that in mind, you might want to accept page_opts (or perhaps use the action_opts vs read_opts suggestion above to encompass the concept)

mxgrn

mxgrn

Recently did some related work, more in the ‘batteries-included’ fashion. Here’s a live demo of a PoC that I prepared for a former client of mine:

https://ash-table-poc.mxgrn.com

It’s note a library (although I did put effort into making the table a reusable component), but maybe some ideas could be an inspiration. E.g. the table auto-configures itself from the Ash resource. Columns can be dragged around and resized, inline editing is supported, etc.

superchris

superchris OP

That’s a really nice example. There’s definitely some bits I’d like to borrow and incorporate. I love how it generates the columns for you and does the whole CRUD lifecycle. Nice work, thanks for sharing that!

superchris

superchris OP

Thanks Zach, having a read_options as assign you can pass definitely makes sense. Supporting multiple pagination strategies also would be great, just got give it a little more thought in terms of how to support it.

superchris

superchris OP

Following @zachdaniel 's suggestion, released 0.2.0 which allows read_options to be passed to specify tenant, actor, etc

zachdaniel

zachdaniel

Creator of Ash

Noice!

Something you might also consider is allowing a list as the sort_key, and constructing the sort_expr automatically if they don’t provide an apply_sort. Would look something like this:

require Ash.Sort

sort = 
  case sort_key do
    value when is_atom(value) or is_string(value) -> 
      {value, direction}
  
    value when is_list(value) -> 
      {path, [name]} = Enum.split(value, -1)

    {Ash.Sort.expr_sort(ref(^path, ^name)), direction}
  end
superchris

superchris OP

Hmm.. that’s really interesting. I wonder if it might be even nicer to lightly parse the sort_key (not married to this name) and if there is a . in it turn it into a path.

zachdaniel

zachdaniel

Creator of Ash

that is clever, I’m into it :+1:

zachdaniel

zachdaniel

Creator of Ash

now that you point it out though, that is just what ash core should do also perhaps. People are typically pretty annoyed to break out expr_sort just for a to_one related field.

its on the roadmap now: Support related sorts via dot separated identifiers in sort · Issue #1272 · ash-project/ash · GitHub

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

Other Trending Topics Top

juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New

We're in Beta

About us Mission Statement