如何将对象传递到 Thymeleaf 中的模式对话框?

mem*_*und 5 java thymeleaf

我有一个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>
Run Code Online (Sandbox Code Playgroud)

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

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

我按如下方式开始,但我缺少如何将人员对象作为对象从单击的行实际传递模式对话框中(以便我可以再次在模式对话框中显示人员字段)。

以下是一种伪代码:

<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>
Run Code Online (Sandbox Code Playgroud)

Ale*_*rin 2

纯百里香叶

要在纯 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>
Run Code Online (Sandbox Code Playgroud)

打开对话框的按钮变为:

<button data-toggle="modal" th:attr="data-target=${'#editModal'+row.id}" data-row="${row}">DEL</button>
Run Code Online (Sandbox Code Playgroud)

使用 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>
Run Code Online (Sandbox Code Playgroud)

删除按钮:

<button class="btn-delete" data-id=${row.id} data-firstname="${row.firstname}" data-lastname="${row.lastname}">DEL</button>
Run Code Online (Sandbox Code Playgroud)

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();
});
Run Code Online (Sandbox Code Playgroud)

  • 好吧,我明白了。但我无法简单地将完整对象传递到模式对话框并使用点表示法进行访问?像 `&lt;div data-toggle="modal" data-target="#deleteModal" th:data-row="${row}"&gt;` 一样,然后在模态对话框中使用 `th:text="{ 访问它row.firstname}"`? (2认同)