ryanzidago

ryanzidago

Editable tabular data / grid / table in Phoenix LiveView?

What’s the canonical way of implementing an editable tabular data / grid / table in Phoenix LiveView?
Think of a spreadsheet but only with a minimal feature set (inline editing, showing select inputs, errors, validations, suggestions, etc.).

I don’t really need multi-collaboration in real time for now.

I have to deal with anywhere between 300 - 600 rows of 30 - 50 columns and probably more in the future.

I have tried the following approaches:

1) Streams + HTML table + form per row

HTML does not allow to have a form per row so it does not work. If I’m using streams, I need to have a form per row so on input change, the Live View gets all the data for the given row, not just for the cell being currently edited.

2) Streams + HTML table + form per table using form attribute on form input

Could not make the change tracking work. The form just sends empty params.

3) Streams + HTML table + form per cell

  • the form will only send one field at a time (except for the field where I explicit add phx-value_some_field but I’d need to repeat that over each input for all of the fields of the record I want to update…)
  • and then in the handle_event("validate"), I would have to do the following:
def handle_event("validate", params, socket) do
  changesets = socket.assigns.record_changesets
  changeset = Enum.find(changesets, &(get_field(&1, :id) == params["id"])
  changeset = Record.changeset(changeset.data, params)
  socket = stream_insert(:changesets, changeset)
  {:noreply, socket}
end

Meaning holding the collection in both the assigns and in the stream, which seems to defeat the purpose of streams in the first place.

4) Streams + indivual form input without any forms

So I have:

<tr>
  <td><.input type="text" value={get_field(changeset, :some_field)} phx-change="validate" phx-value_id={get_field(changeset, :id)} .../></td>
  <td><.input type="select" options={@options} value={get_field(changesetm :some_other_field)} phx-change="validate" phx-value_id={get_field(changesetm :id))} /></td>
</tr>

The problem with stream_insert is that I lose the focus on the cell that was currently being edited. All of the examples that I could find about streams seems to point out that when you want to edit a streamed item, you open a modal, go to another route, etc … I have not seen many use-case for inline editing, which makes me wonder if I am supposed to use streams for that at all.

5) CSS grid

CSS grid does not seem fit for purpose for tabular data. You can’t wrap your headers / subheaders or rows into a parent element and apply classes to the children so it becomes hard to have stuff like highlight the entire row on cell hover. I’m concerned about using CSS grid especially when requirements will grow in complexity in the future.

6) LiveView + JS library (AG-Grid, Handsontable, etc.) via Phoenix hook

Those are either expensive, or I need to duplicate some of the business logic over to JavaScript, which I’d like to avoid as it seems hard to maintain over time.

Most Liked

BartOtten

BartOtten

A post from mobile…so grain of salts are advised.

Replies to listed solutions.

solution 1: How about CSS with display: table? You can wrap divs in one form, no?

Solution 4: You can also add JS hooks to save cursor position and re-activate the cell after stream-insert.


Just freewheeling

assuming you also need some column aggregation / sums.

Create an matrix with input fields with a col-class and row-class. Example class=“b 2”.

Have an event handler send the values to LiveView once a value is entered. Examples just for illustration.

Number 8 in “c 2”: {{c, 2}, :equals, value: 8}
Sum column “g” in “c 3”: {{c, 3}, :calc, [{:sum, {g, nil}}]}

This assumes the whole table is in state and you can query and patch it.

If you don’t want to have state on the server, you can afaik only resort to much more (custom) JS or lazy load rows and cols after dropping the whole table after initial render (but then it gets very complicated im afraid)


That being said: this seems like a task outside the optimal scope for LiveView.

BartOtten

BartOtten

The million dollar question: does column data matter or are we only talking rows?

sreyansjain

sreyansjain

I too have a similar requirement, I am going with the approach of one live component per cell. I am trying to create something like airtable in liveview. So my cell datatype is dynamic.

Last Post!

garrison

garrison

I’m glad to see we’ve reached the point where I don’t even need to reply to these threads anymore :slight_smile: But yes, you are the latest in a long line of victims here.

Thankfully the introduction of keyed :for has substantially improved the situation and the docs now recommend those by default which I am of course very happy about. I don’t know which generators use Streams but those should definitely be changed IMO.

Note that thanks to keyed :for you no longer need to use the LiveComponent hack to get good diffs, though in your particular case (a large grid) I would probably prefer the additional control.

There is very little nuance left in my position at this point; I think Streams should be deprecated. Others (including the Phoenix team) probably disagree with me, though!

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement