如何在 Spring Boot Thymeleaf List 中使用 Bootstrap Modals? [英] How to use Bootstrap Modals with Spring Boot Thymeleaf List?

查看:43
本文介绍了如何在 Spring Boot Thymeleaf List 中使用 Bootstrap Modals?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 

<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}">姓名</h3><!-- 按钮触发模态--><按钮类型=按钮";类=btn btn-二级btn-sm";data-bs-toggle=模态"data-bs-target=#exampleModal"><i class="fas fa-address-card"></i>更多<!-- 模态--><div class="模态淡入淡出";id=exampleModal"tabindex="-1";aria-labelledby="exampleModalLabel";咏叹调隐藏=真">

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

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

<div class="modal-footer"></div>

<p>

解决方案

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

  1. 添加状态变量 到您的循环中.

  2. 使用 ${status.index} 变量为您的目标创建唯一 ID:

     

  3. 使用 ${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...

  1. Add the status variable onto your loop.

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

  2. 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>
    

  3. 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 List 中使用 Bootstrap Modals?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆