简体   繁体   English

基于类ID列表的Django模式显示

[英]Django modal display based on the class id list

I would like to allow the modal to be displayed on the basis of the id set from the database. 我想允许基于从数据库设置的ID显示模式。 Having different fruit/vegetables displayed in the container divided into tiles, I put their id in the id field of the class. 在容器中显示了不同的水果/蔬菜,并将它们分成瓷砖,然后将其ID放在类的ID字段中。

In other words, pressing a button in the overlay class for a particular fruit displays a single modal with the rest of the information. 换句话说,按下覆盖类中特定水果的按钮将显示带有其余信息的单个模式。

HTML CODE: HTML代码:

{% block body_block %}
<div class="container-fruits">
    <div class="row">
        {% for t in thumb %}
            <div class="col-lg-3 col-md-4 col-sm-6 col-xs-12 box">
                {% thumbnail t.image "255x189" crop="center" as im %}
                    <div class="hovereffect">
                        <img class="img-responsive" src="{{ im.url }}" alt="Card image cap">
                        <div class="overlay">
                            <h2>{{ t.name }}</h2>
                            <a id="{{ t.id }}" class="info" data-target="#{{ t.id }}" href="#">show details</a>
                        </div>
                    </div>
                {% endthumbnail %}
            </div>
        {% endfor %}
    </div>
</div>

{% for t in thumb %}
    <div id="{{ t.id }}" class="fruitsModal" tabindex="-1" style="display: none">
        <div class="modal-dialog modal-lg ">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h2 class="modal-title">{{ t.name }} information</h2>
                </div>

                <div class="modal-body">
                    <h2 class="modal-body-text">{{ t.description }}</h2>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
{% endfor %}

<script src="{% static 'javascript/fruits.js' %}"></script>

{% endblock %}

Javascript/Jquery Javascript / jQuery

$(document).ready(function () {
$('.info').click(function (e) {
    $(".fruitsModal").fadeIn('slow');
    var image = $(e.relatedTarget).attr('src');
    var text = $(e.relatedTarget).attr('t.name');
    $(".img-responsive").attr("src", image);
    $(".modal-body").attr("modal-body-text", text);
    console.log('hello bind');

});
$(".btn-default").click(function () {
    $(".fruitsModal").fadeOut('slow');
});
});

To open the modal targeted by a specific ID, you just need to know the ID and 要打开特定ID定位的模式,您只需知道ID和

  • Remove the id id="{{ t.id }}" from your a tag 删除id id="{{ t.id }}"您的a标签
  • Rename your target data-target="#modal-{{ t.id }}" to make it a little better to avoid multiple identical id s. 重命名您的目标data-target="#modal-{{ t.id }}"以使避免多个相同的id更好。

     <a data-toggle="modal" class="info" data-target="#modal-{{ t.id }}".... 
  • Finally rename the id of your modals, like the target above 最后,像上面的目标一样,重命名模态的id

     <div id="modal-{{ t.id }}" class="fruitsModal" tabindex="-1" style="display: none"> 

The tag a becomes: 标签a变为:

<a class="info" data-target="#modal-{{ t.id }}" href="#">show details</a>

And the modal: 和模式:

<div id="modal-{{ t.id }}" class="fruitsModal" tabindex="-1" style="display: none">
    <div class="modal-dialog modal-lg ">

1st Option : 第一种选择

If you need to let bootstrap do everything for you, means when you click on a without your script, the modal will show up. 如果你需要让自举为你做的一切,是指当你点击a没有你的脚本, the modal会显示出来。

Just add the data-toggle attribute to your a tag, and add the same value of data-target to href . 就在添加data-toggle属性的a标签,和值相同添加data-targethref

    <a data-toggle="modal" href="#modal-{{ t.id }}" data-target="#modal-{{ t.id }}"

In that case, the data-target="" is not really needed 在这种情况下,就不需要data-target=""

2nd option : 第二个选择

As you want to do some stuff before showing the modal, this fits your need. 由于您想在显示模态之前做一些事情,所以这符合您的需求。 When triggering the a tag with click event, is js, you just need to show the concerned modal 触发带有click事件a代码是js时,您只需要显示相关的模式


    $('.info').click(function (e) {
        $(".fruitsModal").fadeIn('slow');
        var image = $(e.relatedTarget).attr('src');
        var text = $(e.relatedTarget).attr('t.name');
        $(".img-responsive").attr("src", image);
        $(".modal-body").attr("modal-body-text", text);
        console.log('hello bind');
        // # retrieve the target modal: data-target="#modal-{{ t.id }}"
        var t_modal = $("this").data('target');
        $(t_modal).show(); // this is because you have style={display:none}
        $(t_modal).modal('show');
    });

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

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