首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Thymeleaf中将对象传递给模态对话框?

如何在Thymeleaf中将对象传递给模态对话框?
EN

Stack Overflow用户
提问于 2020-11-17 15:21:19
回答 2查看 7K关注 0票数 3

我有一个thymeleaf页面,它在表中显示数据库内容(人员)。

代码语言:javascript
复制
<tr id="tableBody">
    <td th:text="${row.id}"/>
    <td th:text="${row.firstname}"/>
    <td th:text="${row.lastname}"/>
    <td>
        <button data-toggle="modal" data-target="#editModal" th:data-row="${row}">DEL</button>
    </td>
</tr>

最后一列应该是删除行的按钮。但是在前面,显示一个模态对话框,并删除数据。

问:我如何将完整的排person对象传递给一个模态对话框?

我从下面开始,但是我忽略了如何将person对象从单击的行中作为一个对象传递到模态对话框中(这样我就可以在模态对话框中再次显示person字段)。

以下是一种伪代码:

代码语言:javascript
复制
<div class id="editModal" ...>
  <div class="modal-body">
    <div class="modal-body">
       You are about to delete: <div th:text="${row.firstname}"/> <div th:text="${row.lastname}"/>

       <form action="#" th:action="@{/delete/{id}" th:object="${row}" method="delete">
          <input type="text" hidden="true" th:field="${row.id}">
       </form>
    </div>

    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
        <button type="submit" class="btn btn-primary" th:href="@{/delete/{id}(id=${row.id})}" th:method="delete">Remove</button>
    </div>
  </div>
</div>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-17 15:43:26

纯胸腺

要在纯胸腺网中这样做,您需要为表中的每一行创建一个具有唯一id的对话框,并打开与被删除的行相关联的对话框。

示例情态词:

代码语言:javascript
复制
<div th:each="row : ${rows}" th:attr="id=${'editModal' + row.id}">
  <div class="modal-body">
    <div class="modal-body">
       You are about to delete: <div th:text="${row.firstname}"/> <div th:text="${row.lastname}"/>

       <form action="#" th:action="@{/delete/{id}" th:object="${row}" method="delete">
          <input type="text" hidden="true" th:field="${row.id}">
       </form>
    </div>

    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
        <button type="submit" class="btn btn-primary" th:href="@{/delete/{id}(id=${row.id})}" th:method="delete">Remove</button>
    </div>
  </div>
</div>

打开对话框的按钮变成:

代码语言:javascript
复制
<button data-toggle="modal" th:attr="data-target=${'#editModal'+row.id}" data-row="${row}">DEL</button>

与javascript

如果您可以使用javascript,我建议只创建一个模板的模态对话框使用晶闸管,然后克隆和动态填充它。

示例模态:

代码语言:javascript
复制
<div class id="editModalTemplate">
  <div class="modal-body">
    <div class="modal-body">
       You are about to delete: <div data-value="firstname"/> <div data-value="lastname"/>

       <form action="#" th:action="@{/delete/_id_}" method="delete">
          <input type="text" hidden="true" name="id">
       </form>
    </div>

    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
        <button type="submit" class="btn btn-primary" th:href="@{/delete/{id}(id='_id_')}" th:method="delete">Remove</button>
    </div>
  </div>
</div>

删除按钮:

代码语言:javascript
复制
<button class="btn-delete" data-id=${row.id} data-firstname="${row.firstname}" data-lastname="${row.lastname}">DEL</button>

Javascript (以jQuery实现为例):

代码语言:javascript
复制
$('.btn-delete').click(function(){
    //clone dialog and remove ids to ensure uniqueness
    var $modal = $('#editModalTemplate').clone().removeAttr('id');

    //apply custom values where needed
    var $btn = $(this);
    var rowId = $btn.attr('data-id');
    var firstname = $btn.attr('data-firstname');
    var lastname = $btn.attr('data-lastname');

    $modal.find('[data-value="firstname"]').text(firstname );
    $modal.find('[data-value="lastname"]').text(lastname );
    $modal.find('[name="id"]').val($btn.attr('data-id'));
    $modal.find('form').attr('action').replace('_id_', rowId);     
    $modal.find('button[type="submit"]').attr('href', $modal.find('button[type="submit"]').attr('href').replace('_id_', rowId);

    //show dialog
    $modal.modal();
});
票数 2
EN

Stack Overflow用户

发布于 2021-11-25 08:05:43

我找到了一个轻松的解决方案,希望它能帮助那些正在挣扎的人,诀窍是将这个模式放在表或div的同一个循环中,创建和动态"id“的属性语句,就像在这个页面中解释一个更老的版本:

代码语言:javascript
复制
    <tr th:each="user: ${users}">
        <td ><a data-bs-toggle="modal" data-row="${user}"
                th:attr="data-bs-target='#modal-warning'+${user.id }">Inactivate</a> 
    
    <!--  MODAL -->
  <div th:fragment="modal" class="modal fade" th:id="modal-warning+${user.id }" tabindex="-1"

你也可以查看我的原始帖子这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64878101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档