如何通过rails中的JS请求显示twitter bootstrap模式?

时间:2021-11-21 11:21:10

I want to show a twitter bootstrap modal as a response to a JS request. My show.js.erb function looks something like this:

我想显示一个twitter bootstrap模式作为对JS请求的响应。我的show.js.erb函数看起来像这样:

$(document).ready(function() {

    $('#dialog').modal('show')

});

Here "dialog" is the modal's id. The modal code itself looks something like this:

这里的“对话框”是模态的id。模态代码本身看起来像这样:

<div id="dialog" class="modal hide fade">
    <div class="modal-header">
          <a href="#" class="close">&times;</a>
          <h3> Showing Modal </h3>
    </div>
    <div class="modal-body">
        I need to show up!
    </div>
    <div class="modal-footer">
        <a href="#" class="btn primary">Done</a>
    </div>
</div>

One thing I am sure of is that the javascript is being called. I can have it alter items on the page. Another thing is that modal is working fine. It shows up just fine when I use an HTML button to trigger the request like this:

我确信的一件事是调用了javascript。我可以让它改变页面上的项目。另一件事是模态工作正常。当我使用HTML按钮触发请求时,它显示就好了:

<button data-controls-modal="dialog" data-backdrop="true" data-keyboard="true" class="btn danger"> Show </button>

Any idea why the modal doesn't show up on JS request?

知道为什么modal没有出现在JS请求上吗?

Thank You!

谢谢!

7 个解决方案

#1


38  

I had a similar problem, which I solved with a slight twist

我有一个类似的问题,我解决了一点点扭曲

My modal div is rendered on the calling page (from a partial) and not by the response of the JS request:

我的模态div在调用页面上呈现(来自部分)而不是JS请求的响应:

<div class="modal hide fade" id="modal-window">
  <div class="modal-header">
    <a href="#" class="close">×</a>
    <h3>Loading...</h3>
  </div>
  <div class="modal-body center">
      <%= image_tag "loading.gif" %>
  </div>
  <div class="modal-footer">&nbsp;</div>
</div>

I use this link to rely on rails and Twitter unobtrusive JS:

我使用这个链接依赖rails和Twitter不显眼的JS:

<%= link_to negotiation.name, negotiation_path(negotiation), {:remote => true, 'data-controls-modal' =>  "modal-window", 'data-backdrop' => true, 'data-keyboard' => true} %>

and my show.js.erb looks like this (shortened)

我的show.js.erb看起来像这样(缩短了)

$('.modal-body').html('<%= escape_javascript(render :partial => 'negotiationdetail', :object => @negotiation) %>');
$('.modal-header').remove(); // don't need a header here

This works fine and has the benefit of showing a "loading" animation to the user while the modal is being populated.

这样可以正常工作,并且在填充模态时可以向用户显示“加载”动画。

#2


1  

I had this problem and i fixed it by changing the javascript rendering to:

我有这个问题,我通过将javascript渲染更改为:

$("#modal-window").find(".modal-content").html("<%= j (render 'new') %>");
$("#modal-window").modal();

And the modal window to: 
<div id="modal-window" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content"></div>
  </div>
</div>

You can check more detailed explanation here https://kolosek.com/rails-creating-modals

您可以在https://kolosek.com/rails-creating-modals查看更详细的说明

#3


0  

That code looks like it should work (I'm doing something similar where I show the modal on document ready).

该代码看起来应该可以工作(我正在做类似的事情,我在文档准备就绪时显示模态)。

Check that you're loading the bootstrap-modal.js and make sure it's loading before your custom JS loads. If your custom JS is loading first, you'll see this behavior.

检查您是否正在加载bootstrap-modal.js并确保在加载自定义JS之前加载它。如果您的自定义JS首先加载,您将看到此行为。

#4


0  

I have face the same problem and it has been solved as

我面临同样的问题,它已经解决了

  1. load the jquery files (for example jquery_171_min.js)
  2. 加载jquery文件(例如jquery_171_min.js)
  3. and then load the less js and css and
  4. 然后加载较少的js和css和
  5. load the bootstrap-modal.js
  6. 加载bootstrap-modal.js

try with this sequence.. Hope it works.

尝试这个序列..希望它的工作原理。

#5


0  

This example works very well.

这个例子效果很好。

<body>
    <div id="top"> 
       <a tabindex="-1" href="metadata.html" id="metadata-link" data-target="#modal" data-toggle="modal">Metadata</a>
    </div>

    <div id="modal" class="modal hide fade in" style="display:none;">
      <div class="modal-header">header<a class="close" data-dismiss="modal">x</a></div>
      <div class="modal-body"></div>
      <div class="model-footer">footer</div>
    </div>

 <script>

  $(document).ready(function() {
     $('#top').find('a').trigger('click');

    });
 </script>
</body>

Best regards.

最好的祝福。

#6


0  

I had this problem,

我有这个问题,

the cause for me was setting the modal wholly in partial file and put the trigger (button / link) calling the modal from other view file.

我的原因是将模态完全设置在部分文件中,并将触发器(按钮/链接)从其他视图文件中调用模态。

So let say that the trigger button is in my index view page and I construct the modal in _form.html.erb file; And I want to create a new record from index page using that modal.

那么就说触发按钮在我的索引视图页面中,我在_form.html.erb文件中构建模态;我想使用该模式从索引页面创建一个新记录。

Solution:

解:

  • create the outermost div which has the "id" in the same file as the trigger
  • 创建最外面的div,它在与触发器相同的文件中具有“id”

index.html.erb - trigger button

index.html.erb - 触发按钮

<h3>
  <%= link_to 'Add Category',
    new_todo_path,
    remote: true,
    class: "btn btn-lg btn-success",
    data: {toggle: "modal", target: "#modal_form_category"} 
  %>
</h3>

index.html.erb - outermost of the modal

index.html.erb - 模态的最外层

<div id="modal_form_category" class="modal fade" role="dialog">
</div>

new.js.erb

new.js.erb

$(document).ready(function() {
  $("#modal_form_category").html("<%= j render "form" %>");
});

P.S: pardon the new.js.erb I have no absolute reason enclosing the code untill the page is ready. I guess it will work even without the ready function; if you know is it necessary or not please do tell me.

P.S:原谅new.js.erb我没有绝对的理由将代码封闭,直到页面准备就绪。我想即使没有准备好的功能它也能工作;如果你知道是否有必要请告诉我。

#7


-5  

I am not aware of a jquery "modal" function. It looks like you are trying to get the element#dialog to show up. You could simply do:

我不知道jquery“模态”功能。看起来你正试图让元素#对话框显示出来。你可以这样做:

$("#dialog").show();

Or am I totally missing the question?

还是我完全错过了这个问题?

#1


38  

I had a similar problem, which I solved with a slight twist

我有一个类似的问题,我解决了一点点扭曲

My modal div is rendered on the calling page (from a partial) and not by the response of the JS request:

我的模态div在调用页面上呈现(来自部分)而不是JS请求的响应:

<div class="modal hide fade" id="modal-window">
  <div class="modal-header">
    <a href="#" class="close">×</a>
    <h3>Loading...</h3>
  </div>
  <div class="modal-body center">
      <%= image_tag "loading.gif" %>
  </div>
  <div class="modal-footer">&nbsp;</div>
</div>

I use this link to rely on rails and Twitter unobtrusive JS:

我使用这个链接依赖rails和Twitter不显眼的JS:

<%= link_to negotiation.name, negotiation_path(negotiation), {:remote => true, 'data-controls-modal' =>  "modal-window", 'data-backdrop' => true, 'data-keyboard' => true} %>

and my show.js.erb looks like this (shortened)

我的show.js.erb看起来像这样(缩短了)

$('.modal-body').html('<%= escape_javascript(render :partial => 'negotiationdetail', :object => @negotiation) %>');
$('.modal-header').remove(); // don't need a header here

This works fine and has the benefit of showing a "loading" animation to the user while the modal is being populated.

这样可以正常工作,并且在填充模态时可以向用户显示“加载”动画。

#2


1  

I had this problem and i fixed it by changing the javascript rendering to:

我有这个问题,我通过将javascript渲染更改为:

$("#modal-window").find(".modal-content").html("<%= j (render 'new') %>");
$("#modal-window").modal();

And the modal window to: 
<div id="modal-window" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content"></div>
  </div>
</div>

You can check more detailed explanation here https://kolosek.com/rails-creating-modals

您可以在https://kolosek.com/rails-creating-modals查看更详细的说明

#3


0  

That code looks like it should work (I'm doing something similar where I show the modal on document ready).

该代码看起来应该可以工作(我正在做类似的事情,我在文档准备就绪时显示模态)。

Check that you're loading the bootstrap-modal.js and make sure it's loading before your custom JS loads. If your custom JS is loading first, you'll see this behavior.

检查您是否正在加载bootstrap-modal.js并确保在加载自定义JS之前加载它。如果您的自定义JS首先加载,您将看到此行为。

#4


0  

I have face the same problem and it has been solved as

我面临同样的问题,它已经解决了

  1. load the jquery files (for example jquery_171_min.js)
  2. 加载jquery文件(例如jquery_171_min.js)
  3. and then load the less js and css and
  4. 然后加载较少的js和css和
  5. load the bootstrap-modal.js
  6. 加载bootstrap-modal.js

try with this sequence.. Hope it works.

尝试这个序列..希望它的工作原理。

#5


0  

This example works very well.

这个例子效果很好。

<body>
    <div id="top"> 
       <a tabindex="-1" href="metadata.html" id="metadata-link" data-target="#modal" data-toggle="modal">Metadata</a>
    </div>

    <div id="modal" class="modal hide fade in" style="display:none;">
      <div class="modal-header">header<a class="close" data-dismiss="modal">x</a></div>
      <div class="modal-body"></div>
      <div class="model-footer">footer</div>
    </div>

 <script>

  $(document).ready(function() {
     $('#top').find('a').trigger('click');

    });
 </script>
</body>

Best regards.

最好的祝福。

#6


0  

I had this problem,

我有这个问题,

the cause for me was setting the modal wholly in partial file and put the trigger (button / link) calling the modal from other view file.

我的原因是将模态完全设置在部分文件中,并将触发器(按钮/链接)从其他视图文件中调用模态。

So let say that the trigger button is in my index view page and I construct the modal in _form.html.erb file; And I want to create a new record from index page using that modal.

那么就说触发按钮在我的索引视图页面中,我在_form.html.erb文件中构建模态;我想使用该模式从索引页面创建一个新记录。

Solution:

解:

  • create the outermost div which has the "id" in the same file as the trigger
  • 创建最外面的div,它在与触发器相同的文件中具有“id”

index.html.erb - trigger button

index.html.erb - 触发按钮

<h3>
  <%= link_to 'Add Category',
    new_todo_path,
    remote: true,
    class: "btn btn-lg btn-success",
    data: {toggle: "modal", target: "#modal_form_category"} 
  %>
</h3>

index.html.erb - outermost of the modal

index.html.erb - 模态的最外层

<div id="modal_form_category" class="modal fade" role="dialog">
</div>

new.js.erb

new.js.erb

$(document).ready(function() {
  $("#modal_form_category").html("<%= j render "form" %>");
});

P.S: pardon the new.js.erb I have no absolute reason enclosing the code untill the page is ready. I guess it will work even without the ready function; if you know is it necessary or not please do tell me.

P.S:原谅new.js.erb我没有绝对的理由将代码封闭,直到页面准备就绪。我想即使没有准备好的功能它也能工作;如果你知道是否有必要请告诉我。

#7


-5  

I am not aware of a jquery "modal" function. It looks like you are trying to get the element#dialog to show up. You could simply do:

我不知道jquery“模态”功能。看起来你正试图让元素#对话框显示出来。你可以这样做:

$("#dialog").show();

Or am I totally missing the question?

还是我完全错过了这个问题?