gpt4 book ai didi

elixir - phx-change 后更新表单字段的值

转载 作者:行者123 更新时间:2023-12-03 08:45:02 25 4
gpt4 key购买 nike

我有一个 LiveView 应用程序,可以搜索机场代码。当用户输入 ham 时,它应该将该表单字段的内容替换为 HAM (String.upcase/1),但事实并非如此。但根据我对我的代码的理解应该是这样。我需要更改什么才能用大写版本替换该字段中的所有输入?

顺便说一句:如果我向其添加一个按钮并使用 phx-submit 而不是 phx-change,它就会起作用。但我想让它适用于 phx-change

enter image description here

设置

$ mix phx.new travelagent --live --no-ecto
$ cd travelagent

lib/travelagent_web/router.ex

[...]
scope "/", TravelagentWeb do
pipe_through :browser

live "/", PageLive, :index
live "/search", SearchLive
end
[...]

lib/travelagent/airports.ex

defmodule Travelagent.Airports do
def search_by_code(""), do: []

def search_by_code(code) do
list_airports()
|> Enum.filter(&String.starts_with?(&1.code, code))
end

def list_airports do
[
%{name: "Berlin Brandenburg", code: "BER"},
%{name: "Berlin Schönefeld", code: "SXF"},
%{name: "Berlin Tegel", code: "TXL"},
%{name: "Bremen", code: "BRE"},
%{name: "Köln/Bonn", code: "CGN"},
%{name: "Dortmund", code: "DTM"},
%{name: "Dresden", code: "DRS"},
%{name: "Düsseldorf", code: "DUS"},
%{name: "Frankfurt", code: "FRA"},
%{name: "Frankfurt-Hahn", code: "HHN"},
%{name: "Hamburg", code: "HAM"},
%{name: "Hannover", code: "HAJ"},
%{name: "Leipzig Halle", code: "LEJ"},
%{name: "München", code: "MUC"},
%{name: "Münster Osnabrück", code: "FMO"},
%{name: "Nürnberg", code: "NUE"},
%{name: "Paderborn Lippstadt", code: "PAD"},
%{name: "Stuttgart", code: "STR"}
]
end
end

lib/travelagent_web/live/search_live.ex

defmodule TravelagentWeb.SearchLive do
use TravelagentWeb, :live_view
alias Travelagent.Airports

def mount(_params, _session, socket) do
socket =
socket
|> assign(:airport_code, "")
|> assign(:airports, [])

{:ok, socket}
end

def handle_event(
"airport_code_search",
%{"airport_code" => airport_code},
socket
) do
airport_code = String.upcase(airport_code)

socket =
socket
|> assign(:airport_code, airport_code)
|> assign(:airports, Airports.search_by_code(airport_code))

{:noreply, socket}
end
end

lib/travelagent_web/live/search_live.html.leex

<form phx-change="airport_code_search">
<fieldset>
<label for="nameField">Airport Code</label>
<input type="text" name="airport_code" value="<%= @airport_code %>"
placeholder="e.g. FRA"
autofocus autocomplete="off" />
</fieldset>
</form>

<%= unless @airports == [] do %>
<h2>Search Results</h2>
<table>
<thead>
<tr>
<th>Airport Code</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<%= for airport <- @airports do %>
<tr>
<td><%= airport.code %></td>
<td><%= airport.name %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>

JavaScript 控制台输出

JavaScript Console Output

phx-submit 版本

如果我使用此表单:

<form phx-submit="airport_code_search">
<fieldset>
<label for="nameField">Airport Code</label>
<input type="text" name="airport_code" value="<%= @airport_code %>"
placeholder="e.g. FRA"
autofocus autocomplete="off" />
<input class="button-primary" type="submit" value="Search Airport">
</fieldset>
</form>

我得到了这个工作结果(输入fra并单击按钮后):

enter image description here

PS:对于 JavaScript 或 CSS 的这个问题,有数百万种可能的解决方案。但我想知道如何使用 LiveView 正确解决这个问题。

最佳答案

根据documentation ,

The JavaScript client is always the source of truth for current input values. For any given input with focus, LiveView will never overwrite the input's current value, even if it deviates from the server's rendered updates.

这种情况下的解决方案是使用 JS hooks .

关于elixir - phx-change 后更新表单字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61818154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com