Search code examples
elixirphoenix-frameworkphoenix-live-view

LiveView: How to process data in a rendered comprehension list


In Phoenix, if you need to process the data in the template, you can pass the data to the view for processing to be outputted back to the template. For example:

## index.html.heex
<%= for a <- @articles do %>
  <%= a.title %>
  <%= get_category(a.category_id).name %>

## article_view
def get_category(id) do
  Categories.get_article_category!(id)

However, this is different with LiveView. I've tried to call a component in the comprehension to process the data:

## index.html.heex
<%= for a <- @articles do %>
  <%= a.title %>
  <%= live_component @socket, CategoryComponent, id: a.category_id %>id %>

But now, I'm lost on how to make the Category component (handle_info?) to put the Category names of each article.

What is the proper way to update the data in a rendered list in LiveView?


Solution

  • I could't spend so much time on this so I just used the methods in the context and will put the LiveView queries there

    # index.html.heex
    <%= App.Categories.get_category!(a.category_id).name %>