英文:
How to trigger the modal popup from click a button in ELixir Phoenix
问题
Phoenix 1.7生成器提供了一个位于"CoreComponents"中的模态框。
说明中没有解释如何切换它。我在处理Elixir Phoenix方面不太擅长,正在尝试学习。
基本语法是:
<.modal
id="user-modal"
show >
"show"属性是触发弹出窗口的属性。
在JavaScript中,要触发模态框,通常需要使用布尔值来切换"show"属性的值。在Elixir中,我不知道如何做到这一点,也不知道开发者是否期望采用这种方法来实现。
文档在这里:
<.modal id="confirm-modal">
你确定吗?
<:confirm>确定</:confirm>
<:cancel>取消</:cancel>
</.modal>
JS命令可以传递给`:on_cancel`和`on_confirm`属性,以便调用者对每个按钮按下作出反应,例如:
<.modal id="confirm" on_confirm={JS.push("delete")} on_cancel={JS.navigate(~p"/posts")}>
你确定吗?
<:confirm>确定</:confirm>
<:cancel>取消</:cancel>
</.modal>
这个示例并没有明确显示"show"属性,但在来自Phoenix生成器的下面的工作示例中,使用了"show"属性。
<.modal
:if={@live_action in [:new, :edit]}
id="user-modal"
show
on_cancel={JS.navigate(~p"/users")}
>;
<.live_component
module={AppxWeb.UserLive.FormComponent}
id={@user.id || :new}
title={@page_title}
action={@live_action}
user={@user}
patch={~p"/users"}
/>
</.modal>
我好奇如何思考这个问题的解决过程。正如我所说,在JavaScript中,它是一个事件处理程序和一个布尔值。
"show"属性看起来不像一个字符串,所以我不知道如何以动态方式处理它。
英文:
Phoenix 1.7 generator comes with a modal in "CoreComponents".
The instructions do not explain how to toggle it. I am not very good at working with Elixir Phoenix and I am trying to learn.
The base syntax is:
<.modal
id="user-modal"
show >
The show attribute is what triggers the popup.
In JavaScript, to trigger a modal you typically have a boolean to toggle the "show" attribute value. In Elixir I have no idea how to do that nor do I know if that is the approach that is expected of the developer to make it work.
The documentation is here:
<.modal id="confirm-modal">
Are you sure?
<:confirm>OK</:confirm>
<:cancel>Cancel</:cancel>
</.modal>
JS commands may be passed to the `:on_cancel` and `on_confirm` attributes
for the caller to react to each button press, for example:
<.modal id="confirm" on_confirm={JS.push("delete")} on_cancel={JS.navigate(~p"/posts")}>
Are you sure you?
<:confirm>OK</:confirm>
<:cancel>Cancel</:cancel>
</.modal>
The example does not explicitly show the show attribute, In the working example below from the phoenix generator the show attribute is used.
<.modal
:if={@live_action in [:new, :edit]}
id="user-modal"
show
on_cancel={JS.navigate(~p"/users")}
>
<.live_component
module={AppxWeb.UserLive.FormComponent}
id={@user.id || :new}
title={@page_title}
action={@live_action}
user={@user}
patch={~p"/users"}
/>
</.modal>
I'm curious what the thought process is to think about this problem. Like I said, in JavaScript it's an Event Handler and a boolean.
The show attribute doesn't look like a string so I have no idea how to handle it dynamically.
答案1
得分: 0
这是答案。这是两行代码。使用的是ID,而不是show属性。
<.modal
id="user-modal"
>
</.modal>
<button phx-click={show_modal("user-modal")}> Click me </button>
英文:
Here's the answer. It's two lines of code. The ID is used and not the show attribute.
<.modal
id="user-modal"
>
</.modal>
<button phx-click={show_modal("user-modal")}> Click me </button>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论