Valeriy
Hi guys!
I try CSV Export with Phoenix and LiveView with this guide CSV Export with Phoenix and LiveView - Tutorials and screencasts for Elixir, Phoenix and LiveView (fullstackphoenix.com).
I use filters for data displayed as a table on my heex:
<form phx-change=“filter” class=“pt-4 pb-4 text-sm”>
<span class=“inline-flex”>
<span>
<label>node:</label>
<%= Phoenix.HTML.Form.number_input(:options, :node,
value: @options.node,
“phx-debounce”: “300”,
class: “text-sm border border-slate-300 text-slate-600 w-20 ml-2 mr-4”
) %>
</span>
<span>
<label>date:
<%= Phoenix.HTML.Form.date_input(:options, :date,
value: @options.date,
class: “text-sm border border-slate-300 text-slate-600 ml-2 mr-4”
) %>
</span>
</span>
</form>
and link to export on the same heex:
<.link
href={~p"/export?#{%{message: “journals”, node: @options.node, date: @options.date, sort_by: @options.sort_by, sort_order: @options.sort_order}}“}
method=“post”
class=”-ml-px inline-flex items-center px-3 h-10 border border-slate-300 bg-white text-base font-medium text-slate-600 no-underline hover:bg-slate-300 rounded-md"
>
Export
</.link>
In export controller:
def create(conn, %{“message” => “journals”, “node” => node, “date” => date, “sort_by” => sort_by, “sort_order” => sort_order}) do
…
csv_data = …conn
|> put_resp_content_type(“text/csv”)
|> put_resp_header(“content-disposition”, “attachment; filename="journals.csv"”)
|> put_root_layout(false)
|> send_resp(200, csv_data)
end
CSV export works fine, but i have some quiestion: the filter (form) stops responding to changes after loading csv-file, but all other links work (pagination and sorting).
It seems that “phx-change” don’t send any messages to my live_view.
I also read this topics, but could not find an answer to my question:
- Changing form contents on
phx-change- Phoenix Forum / Questions / Help - Elixir Programming Language Forum (forum.elixirforum.com) - Form not firing phx-change in LiveView - Phoenix Forum / Questions / Help - Elixir Programming Language Forum (forum.elixirforum.com)
Thank you.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
codeanpeace
Off the top of my head, I suspect what’s going on here is that the LiveView socket disconnects on export since clicking a default generated
<.link href=...>results in traditional browser navigation. You can check if this is the case by using thephx-disconnectedbinding to show/do something.If that is the case, you could try specifying the anchor tag’s
downloadattribute and/ortargetattribute as new tab and see if that makes a difference.Updated to add:
https://github.com/phoenixframework/phoenix_live_view/issues/2552
Valeriy
Thank you codeanpeace!
target=“_blank” works fine, but this causes the opening of a blank page for a short time. It seems not very nice.
I couldn’t get download attribute to work. I try this:
or
Do you have any thoughts on this?
codeanpeace
Hmm, could you elaborate a bit more? Are you seeing an error? Does no download take place? What does the rendered html of the
aanchor tag look like?The MDN docs list linked above mentions some requirements for the
downloadattribute for anchor elements and different browsers may handle things a bit differently as well.Valeriy
As I can see, there are no errors.
Dev-tools console output:
…
phx-F46Bx0s-9TzePR4i socket: disconnect for page nav - undefined
phx-F46Bx0s-9TzePR4i destroyed: the child has been removed from the parent - undefined
destroyed: the child has been removed from the parent - undefined
Yes, it works well.
HTML code for:
looks like this:
I try it in two browsers: edge and chrome, the behavior in both cases is the same.
P.S. Just in case:
codeanpeace
That’s odd, LiveView shouldn’t disconnect when the
downloadattribute as of this PR: Fix for #2552: do not unload LiveView socket on `download` link clicks. by sherbondy · Pull Request #2611 · phoenixframework/phoenix_live_view · GitHubI’d suggest taking a few steps back and directly specifying the anchor tag with the download attribute temporarily just to see if that changes anything and get it working as expected.
Valeriy
I try this:
The behavior is the same:
Perhaps some other settings are required somewhere else?
Valeriy
I find this code in phoenix_live_view/assets/js/phoenix_live_view/dom.js:
I additionally tried simple GET (not POST):
and got the same result: download file works well, but “socket: disconnect for page nav” and “destroyed: the child has been removed from the parent”.
Valeriy
BTW (for FF and chrome):
e.ctrlKey - don’t work in my case
e.shiftKey - works, but open a blank page
download - don’t work in my case
target=“_blank” - works, but open a blank page for a short time