Bootstrap模式不更新模态内容

问题描述 投票:1回答:1

我正在尝试创建一个模型表,旁边有一个按钮,它打开一个模态并在表单视图中具有相同的模型行。该表正在正确填充,但正在创建的n个引导模式仅保存第一个可迭代模型值。是因为bootstrap在呈现页面时只加载一次模态的内容?我该怎么做才能解决这个问题?我应该运行一个函数来根据模型数据更新模态内容吗?

随意提出更多澄清。

{% extends 'base.html' %}

{% load static %}

{% block content %}
    <table>
        {% for item in data %}

            <tr>
                <th>From</th>
                <th>To</th>
                <th>Weight</th>
                <th>Length</th>
                <th>Type</th>
                <th>Material Type</th>
                <th>Number of Trucks</th>
                <th>Loading Time</th>
            </tr>
            <tr>
                <td>{{ item.From }}</td>
                <td>{{ item.To }}</td>
                <td>{{ item.Weight }}</td>
                <td>{{ item.Length }}</td>
                <td>{{ item.Type }}</td>
                <td>{{ item.MaterialType }}</td>
                <td>{{ item.Numberoftrucks }}</td>
                <td>{{ item.Loadingtime }}</td>
                <td>
                    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Bid
                        now! for id {{ item.id }} </button>
                </td>
                {#        {% endfor %}#}


                <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                        aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                            </div>
                            <div class="modal-body">

                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.To }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.From }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Weight }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Length }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Type }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.MaterialType }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Numberoftrucks }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Loadingtime }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here...">Bid
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                <button type="button" class="btn btn-primary">Save changes</button>
                            </div>

                        </div>
                    </div>
                </div>

            </tr>
        {% endfor %}
    </table>

{% endblock %}
python django twitter-bootstrap forms bootstrap-modal
1个回答
1
投票

你可以通过将model.pk添加到模态id来修复它

在按钮中

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal"
     data-target="#myModal{{ item.id }}">
    Bid now! for id {{ item.id }}
</button>

并在

 <div class="modal fade"
      id="myModal{{ item.id }}"
      tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

其他解决方案是创建js函数以在激活模态时每次加载新数据。

© www.soinside.com 2019 - 2024. All rights reserved.