简体   繁体   English

Rails,Bootstrap,尝试动态加载模式无法正常工作

[英]Rails, Bootstrap, trying to dynamically load modal does not work as expected

OK I have a link to modal in html, which works perfectly. 好的,我有一个到html模态的链接,效果很好。 As you can see the link in there also, but I don't need it in my app. 如您所见,该链接也位于其中,但我的应用程序中不需要它。

<div class="block">
    <a data-toggle="modal" class="btn btn-danger btn-block" role="button" href="#editor-modal">Run modal with WYSIWYG editor</a>

    <!-- Modal with WYSIWYG editor -->
    <div aria-hidden="true" style="display: none;" id="editor-modal" class="modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title"><i class="icon-quill2"></i> WYSIWYG editor inside modal</h4>
                </div>



                <div class="modal-footer">
                    <button class="btn btn-warning" data-dismiss="modal">Close</button>
                    <button class="btn btn-primary">Save</button>
                </div>
            </div>
        </div>
    </div>
    <!-- /modal with WYSIWYG editor -->

</div>

Instead of having a ton of modals in my app, I want to load them dynamically. 我想动态加载它们,而不是在我的应用程序中添加大量的模态。

I tried to define a placeholder and load the modal there and then show it. 我试图定义一个占位符并在那里加载模式,然后显示它。 I have done this a couple of times, but this time it does not work for some reason. 我已经做过几次,但是这次由于某种原因它不起作用。

So in my index.html.erb I got 所以在我的index.html.erb中,

<div id="modal-placeholder" class="block"> </div>

I got my partial _addNewModal.html.erb : 我得到了部分_addNewModal.html.erb

<!-- Modal with WYSIWYG editor -->
<div aria-hidden="true" style="display: none;" id="editor-modal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title"><i class="icon-quill2"></i> WYSIWYG editor inside modal</h4>
            </div>



            <div class="modal-footer">
                <button class="btn btn-warning" data-dismiss="modal">Close</button>
                <button class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>
<!-- /modal with WYSIWYG editor -->

And I load it with the following code. 然后用以下代码加载它。

$('#modal-placeholder').html("<%= j render 'news/modals/addNewModal'%>")
$('#editor-modal').modal('toggle');

I don't understand what is wrong here. 我不明白这里出了什么问题。 I tried to replicate the html code from my template, but for some reason it does not work. 我试图从模板中复制html代码,但是由于某些原因,它无法正常工作。

Any help appreciated. 任何帮助表示赞赏。

Edit: As I checked, the modal-placeholder actually is filled with the data, but the modal does not show up.I tried both 编辑:正如我检查的那样, 模态占位符实际上充满了数据,但模态没有显示出来。

$('#editor-modal').modal('toggle'); 

&

$('#editor-modal').modal('toggle');

Any ideas? 有任何想法吗?

The j render by default search for a js.erb file 默认情况下, j渲染搜索js.erb文件

When rendering partials inside Javascript code, you should use the escape_javascript method, like so 在Javascript代码中呈现部分内容时,应使用escape_javascript方法,如下所示

$('#modal-placeholder').html("<%= escape_javascript render(:partial => 'news/modals/addNewModal') %>");

If you explicitly declare the partial key then it will search for your html.erb file instead of js.erb 如果您显式声明了partial密钥,则它将搜索html.erb file instead of js.erb

So try either the above command or this. 因此,请尝试以上命令或此命令。

$('#modal-placeholder').html("<%= j render(:partial => 'news/modals/addNewModal') %>");

For the toggling, remove inline display: none; 对于切换,删除行内display: none; from the form and add a style for it, 从表单中添加样式,

#editor-modal
 {
   display: none;
 }

Then the toggle will work. 然后切换将起作用。

$('#editor-modal').toggle();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM