LiveView:如何处理渲染理解列表中的数据
在Phoenix中,如果需要处理模板中的数据,可以将数据传递给视图进行处理,然后输出回模板。例如:
## 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)
但是,这与 LiveView 不同。我尝试在理解中调用一个组件来处理数据:
## index.html.heex
<%= for a <- @articles do %>
<%= a.title %>
<%= live_component @socket, CategoryComponent, id: a.category_id %>id %>
但是现在,我不知道如何使类别组件(handle_info?)来放置每篇文章的类别名称。
在 LiveView 中更新渲染列表中的数据的正确方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我不能花太多时间在这上面,所以我只是使用了上下文中的方法,并将 LiveView 查询放在那里
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