Rails 中的动态 Fancybox 表单

发布于 2024-11-06 07:48:07 字数 1182 浏览 0 评论 0原文

谁能向我指出一些简单的分步指南,帮助我了解如何将 Fancybox 和 Ajax 结合在一起?

我有三个模型:

Class Event
    belongs_to :site
    belongs_to :operator
End

Class Site
    has_many :events
End

Class Operator
   has_any :events
End

用户可以添加新事件,然后从选择框中选择站点或操作员。我还为站点和操作员创建了快速添加部分。它们呈现在“事件”表单上的 div 中,并在单击链接时显示在 Fancybox 中。

<%= link_to_box "Add", "#site" %>
<%= link_to_box "Add", "#operator" %>

<div id="site">
    <%= render 'sites/quickadd' %>
</div>

<div id="operator">
    <%= render 'operators/quickadd' %>
</div>

到目前为止,一切都很好。

现在我有两个问题。

1-如何隐藏事件表单上的快速添加 div,但在 Fancybox 中显示它们。 CSS 类(例如 display:none 或visibility:hidden)会导致部分内容不显示在任一位置。目前,部分内容在事件表单的末尾以及 Fancybox 弹出窗口中呈现,但这并不理想。

2-如何设置这些快速添加部分,以便它们动态更新事件表单。例如,我正在为网站 Foobar 添加一个新事件。 Foobar 在选择框中不可用,因此我单击“添加”,在弹出表单中输入 Foobar,单击保存,Foobar 会自动设置在“事件”表单上的选择框中。

我假设问题 2 将涉及 Ajax 并调用 remote =>;正确。然而,我对此非常陌生,确实需要一个基本的分步指南来帮助我了解如何实现这一点。例如,“添加”链接是否需要是远程链接,还是部分链接?如果是部分内容,我该如何编码?保存后,如何更新和设置父表单选择框?

就像我说的,基本的东西,但是在阅读了 Fancybox 和其他类似灯箱的弹出窗口的几份指南以及 Ajax 的几份指南后,我仍然无法将两者联系在一起。

感谢您的指点。

Can anyone point me towards some simple step by step guides that would help me to understand how to couple Fancybox and Ajax together?

I have three models:

Class Event
    belongs_to :site
    belongs_to :operator
End

Class Site
    has_many :events
End

Class Operator
   has_any :events
End

Users can add a new event, and select Site or Operator from select boxes. I've also created a quick-add partials for Site and Operator. These are rendered in a div on the Event form, and displayed in Fancybox when a link is clicked.

<%= link_to_box "Add", "#site" %>
<%= link_to_box "Add", "#operator" %>

<div id="site">
    <%= render 'sites/quickadd' %>
</div>

<div id="operator">
    <%= render 'operators/quickadd' %>
</div>

So far so good.

Now I have two questions.

1- How to I hide the quick-add divs on the Event form, but display them in Fancybox. CSS classes such as display:none or visibility:hidden result in the partials not displaying in either location. Currently the partials are rendered at the end of the Event form as well as in the Fancybox popup, but this is not ideal.

2- How do I setup these quickadd partials so that they dynamically update the Event form. For example I'm adding a new Event for site Foobar. Foobar is not available in the select box so I click "add", enter Foobar in the popup form, click save, and Foobar is automatically set in the select box on the Event form.

I assume that question 2 will involve Ajax and calls for remote => true. However I'm very new to this and really need a basic step by step guide that would help me understand how to implement this. For example, do the "add" links need to be remote, or the partials? If the partials, how do I code that? After save, how do I update and set the parent form select box?

Like I said, basic stuff, but having read several guides for Fancybox and other lightbox-like popups, and several guides for Ajax, I'm still having trouble tying the two together.

Thank you for any pointers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法少女 2024-11-13 07:48:07

1)如果它没有正确显示和隐藏div,那么我会将它们设置为display:none;在 css 中并在您的 fancybox 调用中使用类似的内容

$("#site").fancybox({
    onStart     :   function() {
        $('#DIV').show();
    },

    onClosed    :   function() {
                $('#DIV').hide();
    }
});

2)当我有一个像您所描述的表单时,在您完成提交表单(我将在表单标记上使用 :remote => true 提交该表单)时,您可以它会检查 ajaxComplete,并且您可以在 create.js.erb 或 update.js.erb 文件上设置一些变量,这些变量将显示和隐藏表单、设置当前索引,或者您可能必须在 ajaxCompleted 为“后重新加载列表”已完成”

不确定这是否有帮助或提供方向,但希望它能提供一些指导..今天晚些时候可能会发布更多内容来提供帮助

1) If it isn't showing and hiding the divs correctly for you, then I would put them as display:none; in the css and use something like this for your fancybox call

$("#site").fancybox({
    onStart     :   function() {
        $('#DIV').show();
    },

    onClosed    :   function() {
                $('#DIV').hide();
    }
});

2) When I have a form like your describing, after you are done submitting the form (which I would submit using :remote => true on the form tag, you can have it check for ajaxComplete. and you can set some variables on your create.js.erb or update.js.erb files that will show and hide forms, set the current index, or you might have to reload your list after the ajaxCompleted is "completed"

Not sure if this helps or gives direction, but hope it will give some guidance..might be able to post more later today to help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文