如何在 Spring Boot Thymeleaf 列表中使用 Bootstrap 模式?

Posted

技术标签:

【中文标题】如何在 Spring Boot Thymeleaf 列表中使用 Bootstrap 模式?【英文标题】:How to use Bootstrap Modals with Spring Boot Thymeleaf List? 【发布时间】: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" >
                    </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>

【问题讨论】:

【参考方案1】:

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

    将status variable 添加到您的循环中。

    <div th:each="user, status : $listUsers" class="card mb-3">
    

    使用$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>
    

    使用$status.index 变量为您的模态创建一个唯一的ID:

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

【讨论】:

我什至不知道如何感谢你。我整天都在寻找解决方案,而您的解决方案在一分钟内解决了。非常感谢。

以上是关于如何在 Spring Boot Thymeleaf 列表中使用 Bootstrap 模式?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Spring Boot 中使用 Thymeleaf 保存角色?

Spring boot 如何将 Thymeleaf 视图解析器放入其 bean 容器中

如何使用 Thymeleaf 或 JSP 在 Spring Boot 中启用目录列表

如何使用 Thymeleaf 配置 Spring Boot 并使用 sec:authentication 标签

spring boot使用thymeleaf

如何使用 Thymeleaf 在 Spring Boot 应用程序中正确缓存数据