【问题标题】:How to pass object to modal dialog in Thymeleaf?如何将对象传递给 Thymeleaf 中的模态对话框?
【发布时间】:2020-11-17 15:21:19
【问题描述】:

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

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

最后一列应该是删除该行的按钮。但在此之前,显示一个模式对话框,其中包含正在删除的数据。

问题:如何将整行人员对象传递给模态对话框?

我开始如下,但我错过了如何将人员对象从单击的行传递到模态对话框作为对象(这样我就可以在模式对话框)。

以下是一种伪代码:

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

【问题讨论】:

  • 您需要纯 html+tymeleaf 解决方案还是可以接受 javascript?
  • 我更喜欢纯百里香的解决方案,没有js。

标签: java thymeleaf


【解决方案1】:

纯百里香

要在纯 thymeleaf 中执行此操作,您需要为表中具有唯一 ID 的每一行创建一个对话框,并打开与要删除的行关联的对话框。

模态示例:

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

打开对话框的按钮变成:

<button data-toggle="modal" th:attr="data-target=${'#editModal'+row.id}" data-row="${row}">DEL</button>

使用 javascript

如果您可以使用 javascript,我建议您只使用 thymeleaf 创建模态对话框的模板,然后克隆它并动态填充它。

模态示例:

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

删除按钮:

<button class="btn-delete" data-id=${row.id} data-firstname="${row.firstname}" data-lastname="${row.lastname}">DEL</button>

Javascript(以jQuery实现为例):

$('.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();
});

【讨论】:

  • 你能给出一个使用js的替代例子吗?可能这会涉及jquery?但如果以上是唯一的解决方案,我宁愿选择 js 解决方案。我想每页显示 100 行,所以我不能去生成 100 个隐藏的模式对话框。
  • @membersound 我用 jQuery 添加了一个示例
  • 好的,我明白了。但是我无法简单地将整个对象传递给模态对话框,并且访问是使用点符号吗?喜欢&lt;div data-toggle="modal" data-target="#deleteModal" th:data-row="${row}"&gt;,然后在模态对话框中使用th:text="{row.firstname}"访问它?
  • 因为我认为它确实是样板文件,所以我必须为删除按钮显式重复所有人员属性,例如 data-id=, data-firstname=, data-lastname=...。我希望我可以传递完整的th:data-row="{row} 对象,然后提取jquery 函数中的字段?
  • 您不能将完整对象存储为 html 属性,但可以将其存储为 javascript 变量 var allRows = [[${rows}]];。这样,您只需将行的 id 存储在 html 中,在 jQuery 中您可以从 allRows 变量中获取其余数据
【解决方案2】:

我找到的轻松解决方案,希望对谁有帮助,诀窍是将模态放在表格或div的同一个循环中,并使用百里香的属性句创建动态“id”,就像是在此page 中解释了旧版本:

    <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"

你也可以查看我的原帖here

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • 你说得对,我更新我的答案
  • 那么实际上您正在创建 N 个版本的模态对话框?我认为如果您要显示 100 多行,这可能效率低下,即使默认情况下模式是隐藏的。
  • 实际上没有,在我的情况下,首先是因为视图是分页的,另一方面,如果某些条件与“th:if”命令匹配,我只会创建模式。
猜你喜欢
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 2013-07-06
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多