The server side of the pickers in a LiveView: the searches of the
pickers with search, and the events that change a picker from the
server.
defmodule MyAppWeb.TaskLive do
use MyAppWeb, :live_view
on_mount Erabi.Live
...
end(or on_mount Erabi.Live in a live_session). The LiveView then
answers the searches of its pickers itself: the app writes no event
clauses.
Search
A picker's search is a function of the query:
<.combobox field={@form[:city_id]} search={&Places.cities/1} selected={@city} />
def cities(query), do: Repo.all(from c in City, where: ilike(c.name, ^"%#{query}%"), limit: 50)It gives options (anything a picker takes, see Erabi.Option) for the
query, in the order to show them; an empty query is "as the list
opens" (recent or suggested options). The picker shows the first 50,
and the next 50 as the list scrolls.
A function of two arguments gets the page too, %{offset: n, limit: 50}, and gives {options, more?}: for a search that pages in the
database.
The search runs in the LiveView's process, so a slow one holds it: with
async, it runs in a task (start_async) and its answer comes when it
is ready. A query that changed meanwhile drops the older answer.
To rank options as the picker does in the browser, use
Erabi.Match.rank/2.
Summary
Functions
Closes a picker's list.
Moves the focus to a picker.
Opens a picker's list.
Forgets the cached answers of a picker's search: its options on the server changed.
Sets the value of a picker from the server (as text, as the picker
posts it), without a phx-change: for a value the server changed while
the picker is on the page.
Functions
@spec close(Phoenix.LiveView.Socket.t(), String.t()) :: Phoenix.LiveView.Socket.t()
Closes a picker's list.
@spec focus(Phoenix.LiveView.Socket.t(), String.t()) :: Phoenix.LiveView.Socket.t()
Moves the focus to a picker.
@spec open(Phoenix.LiveView.Socket.t(), String.t()) :: Phoenix.LiveView.Socket.t()
Opens a picker's list.
@spec refresh(Phoenix.LiveView.Socket.t(), String.t()) :: Phoenix.LiveView.Socket.t()
Forgets the cached answers of a picker's search: its options on the server changed.
@spec set(Phoenix.LiveView.Socket.t(), String.t(), term()) :: Phoenix.LiveView.Socket.t()
Sets the value of a picker from the server (as text, as the picker
posts it), without a phx-change: for a value the server changed while
the picker is on the page.