s-stepien

s-stepien

Hello

On the index page I have a search box that user can use to type a filter query:

<form phx-change="search" class="search-form">
  <%= search_input :search_field, :query, placeholder: "Wyszukaj...", "phx-debounce": "300", value: @query %>
</form>

During typing, I’m generating phx-change (using debounce) that I handle in the event handler. That handler will generate updated assignments for the socket and the filtered view of the index page will be shown. And that works.
However, I need to somehow store the filter query string so it can be used also on the sub-pages too. Currently I’m storing the query string to the database (to given user’s row) and use it on sub-pages when I need.
And there comes the problem that I have: when user goes from sub-page to the index page (using browser back button or link provided on the sub-page [“back”]) the view of index is displayed as filter is not there (but it is there, since the input sets the value using: <%= search_input ...., "phx-debounce": "300", value: @query %>). So basically the phx-change event is not generated in that case.

How to overcome this? Maybe my architectural approach is incorrect in this context?

Showing Posts 1 to 5

s-stepien

s-stepien OP

Well, OK I’ve fixed my problem by using the query in the mount function.

But I would like to hear from you, if this architectural approach is nice or not :wink:

trisolaran

trisolaran

I hope I understood your problem. The way I deal with this kind of issues is as follows:

  1. query string in URL changes → perform_search and update liveview

  2. phx-change event → issue a push_patch with a query string corresponding to the new search input. The push_patch updates the query string and triggers (1)

This way the state of your view, search input and query string in the URL are always in sync.

In (simplified) code:

# This handles (1)
def handle_params(params, _uri, socket) do
    search_input = params["search_field"] <-- This comes from the URL

    {
      :noreply,
      assign(
        socket,
        seach_results: peform_search(search_field)
       )
    }
  end

# This handles (2)
def handle_event(
        "search",
        %{"search_field" => search_field}, <-- This comes from the form input
        socket
      ) do

    {:noreply,
     push_patch(socket,
       to: Routes.api_path(socket, :your_live_view_page_name, search_field: search)
     )}
end 

# This handles the initial mount
def mount(params, _session, socket) do

    {:noreply, socket} = handle_params(params, "", socket)

    {
      :ok,
      socket
    }
  end 

I hope this is what you were looking for.

s-stepien

s-stepien OP

Hm…I understand your approach here but how in this, the “persistence” comes into play? I do want to use the filter on index live_view page but also on foo and bar sub-pages.

trisolaran

trisolaran

Could you elaborate a bit more on what you mean by “subpages” and how they relate to the index page? Is the search input field supposed to be visible on every page of your site?

sfusato

sfusato

You could generate all your links passing in a @global_query_params assign in your live link generators. Then in mount/handle_params have a helper that will retrieve, sanitize and set it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews