【问题标题】:How to use Bootstrap Modals with Spring Boot Thymeleaf List?如何在 Spring Boot Thymeleaf 列表中使用 Bootstrap 模式?
【发布时间】:2021-06-28 14:29:12
【问题描述】:

我列出了我的用户。它完美运行,每个用户都有自己的 Bootstrap 卡。但是当我点击“更多”来触发模态时,每个“更多”按钮都会给出相同的模态,这是用户#1的弹出窗口。所以无论我对哪个用户的数据感兴趣,它总是显示相同的模态。我希望它能够正常工作,这样每当我点击更多按钮时,窗口就会弹出该用户的数据。

    <div th:each="user : ${listUsers}" class="card mb-3">
                <div class="row g-0">
                    <!--  <div class="col-md-3">
                        <img src="/images/noimage2.png" alt="...">
                    </div>-->
                    <div class="col-md-12">
                        <div class="card-body">
                            <h3 class="card-title"
                                th:text="${user.name}">Name</h3>

                            <!-- Button trigger modal -->
                            <button type="button" class="btn btn-secondary btn-sm"
                                data-bs-toggle="modal" data-bs-target="#exampleModal">
                                <i class="fas fa-address-card"></i> More
                            </button>

                            <!-- Modal -->
                            <div class="modal fade" id="exampleModal" tabindex="-1"
                                aria-labelledby="exampleModalLabel" aria-hidden="true">
                                <div
                                    class="modal-dialog modal-dialog-scrollable modal-lg modal-dialog-centered">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="exampleModalLabel"
                                                th:text="${user.name}">Modal title</h5>
                                            <button type="button" class="btn-close"
                                                data-bs-dismiss="modal" aria-label="Close"></button>
                                        </div>
                                        <div class="modal-body">

                                            <span class="d-inline"
                                                th:text="${user.address}"></span> | <span
                                                th:text="${user.phoneNumber}"></span> | <span
                                                th:text="${user.email}">email</span>
                                        </div>

                                        <p class="card-body"
                                            th:text="${user.description}"></p>
                                    </div>
                                    <div class="modal-footer"></div>

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

【问题讨论】:

    标签: spring-boot modal-dialog thymeleaf bootstrap-5


    【解决方案1】:

    你所有的按钮都指向#exampleModal——你可以在这里看到:data-bs-target="#exampleModal"。如果你想让按钮工作,你需要给你的每个模态一个唯一的 id,然后用正确的按钮定位正确的模态。所以...

    1. status variable 添加到您的循环中。

      <div th:each="user, status : ${listUsers}" class="card mb-3">
      
    2. 使用${status.index} 变量为您的目标创建一个唯一的ID:

       <button type="button" class="btn btn-secondary btn-sm"
               data-bs-toggle="modal" th:data-bs-target="|#userModal${status.index}|">
         <i class="fas fa-address-card"></i> More
       </button>
      
    3. 使用${status.index} 变量为您的模态创建一个唯一的ID:

       <div class="modal fade" th:id="|#userModal${status.index}|" tabindex="-1"
         aria-labelledby="exampleModalLabel" aria-hidden="true">
      

    【讨论】:

    • 我什至不知道如何感谢你。我整天都在寻找解决方案,而您的解决方案在一分钟内解决了。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-11-08
    • 2018-08-15
    • 2019-09-24
    • 2017-12-06
    • 1970-01-01
    • 2020-01-31
    • 2016-01-26
    相关资源
    最近更新 更多