如何从单击按钮在Elixir Phoenix中触发模态弹出窗口

huangapple go评论51阅读模式
英文:

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:

  &lt;.modal
  id=&quot;user-modal&quot;
  show &gt;

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:

      &lt;.modal id=&quot;confirm-modal&quot;&gt;
        Are you sure?
        &lt;:confirm&gt;OK&lt;/:confirm&gt;
        &lt;:cancel&gt;Cancel&lt;/:cancel&gt;
      &lt;/.modal&gt;

  JS commands may be passed to the `:on_cancel` and `on_confirm` attributes
  for the caller to react to each button press, for example:

      &lt;.modal id=&quot;confirm&quot; on_confirm={JS.push(&quot;delete&quot;)} on_cancel={JS.navigate(~p&quot;/posts&quot;)}&gt;
        Are you sure you?
        &lt;:confirm&gt;OK&lt;/:confirm&gt;
        &lt;:cancel&gt;Cancel&lt;/:cancel&gt;
      &lt;/.modal&gt;

The example does not explicitly show the show attribute, In the working example below from the phoenix generator the show attribute is used.



&lt;.modal
  :if={@live_action in [:new, :edit]}
  id=&quot;user-modal&quot;
  show
  on_cancel={JS.navigate(~p&quot;/users&quot;)}
&gt;

&lt;.live_component
    module={AppxWeb.UserLive.FormComponent}
    id={@user.id || :new}
    title={@page_title}
    action={@live_action}
    user={@user}
    patch={~p&quot;/users&quot;}
  /&gt;
&lt;/.modal&gt;

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属性。

&lt;.modal
    id=&quot;user-modal&quot;
&gt;
&lt;/.modal&gt;

&lt;button phx-click={show_modal(&quot;user-modal&quot;)}&gt;  Click me &lt;/button&gt;
英文:

Here's the answer. It's two lines of code. The ID is used and not the show attribute.

 &lt;.modal
    id=&quot;user-modal&quot;
    &gt;
  &lt;/.modal&gt;


&lt;button phx-click={show_modal(&quot;user-modal&quot;)}&gt;  Click me &lt;/button&gt;

huangapple
  • 本文由 发表于 2023年3月3日 23:44:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定