如何在 Spring Boot Thymeleaf 列表中使用 Bootstrap 模式? [英] How to use Bootstrap Modals with Spring Boot Thymeleaf List?
问题描述
我列出了我的用户.它完美运行,每个用户都有自己的 Bootstrap 卡.但是当我点击更多"时触发模态,每更多"按钮给出相同的模态,这是用户 #1 的弹出窗口.所以无论我对哪个用户的数据感兴趣,它总是显示相同的模态.我希望它能够正常工作,这样每当我点击更多按钮时,窗口就会弹出该用户的数据.
<div th:each="user : ${listUsers}"类=卡mb-3"><div 类=行 g-0"><!-- <div class="col-md-3"><img src="/images/noimage2.png"alt="..."></div>--><div 类=col-md-12"><div 类=卡体"><h3 class=卡片标题"th:text="${user.name}">名称</h3><!-- 按钮触发模式--><按钮类型=按钮"class="btn btn-secondary btn-sm"data-bs-toggle=模态";data-bs-target="#exampleModal"><i class="fas fa-address-card"></i>更多的</按钮><!-- 模态--><div 类=模态淡入淡出";id="exampleModal";tabindex="-1";aria-labelledby=exampleModalLabel";aria-hidden="true">
你所有的按钮都指向 #exampleModal
-- 你可以在这里看到:data-bs-target="#exampleModal"
.如果你想让按钮工作,你需要给你的每个模态一个唯一的 id,然后用正确的按钮定位正确的模态.所以...
添加 状态变量 进入你的循环.
使用
${status.index}
变量为您的目标创建唯一 id:<button type="button";class="btn btn-secondary btn-sm"data-bs-toggle=模态";th:data-bs-target="|#userModal${status.index}|"><i class="fas fa-address-card"></i>更多的</按钮>
使用
${status.index}
变量为您的模式创建一个唯一的 id:I list out my users. It works perfectly, every user has their own Bootstrap card. But when I click on "more" to trigger the Modal, every "more" button gives the same Modal, which is the pop-up window for User #1. So no matter which user's data interests me, it always shows the same Modal. I want it to work properly, so that whenever I click on the more button the window pops up with that user's data.
<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>
解决方案All of your buttons are targetting
#exampleModal
-- you can see here:data-bs-target="#exampleModal"
. If you want the buttons to work, you need to give each of your modals a unique id and then target the correct modal with the correct button. So...Add the status variable onto your loop.
<div th:each="user, status : ${listUsers}" class="card mb-3">
Use the
${status.index}
variable to create a unique id for your target:<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>
Use the
${status.index}
variable to create a unique id for your modal:<div class="modal fade" th:id="|#userModal${status.index}|" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
这篇关于如何在 Spring Boot Thymeleaf 列表中使用 Bootstrap 模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文