Thymeleaf - 用确认模态删除

时间:2018-02-27 13:28:19

标签: java spring web thymeleaf

我想从表中删除该对象。 问题是我没有删除我点击的对象。我总是删除上面的对象。我做错了什么?

home.html的

<tr th:each="fgp : ${fingerprints}">
    <td><a th:href="@{'/home/'+${fgp.fingerprintId}+'/viewFingerprints'}"><button
                        type="button" class="btn btn-success">V</button></a> <a
                th:href="@{'/home/'+${fgp.fingerprintId}+'/editFingerprints'}"><button
                        type="button" class="btn btn-warning">E</button></a>

                <button type="button" class="btn btn-danger" data-toggle="modal"
                    data-target="#myModal"></button>

                <!-- The Modal -->
                <div class="modal fade" id="myModal">
                    <div class="modal-dialog">
                        <div class="modal-content">

                            <!-- Modal Header -->
                            <div class="modal-header">
                                <h4 class="modal-title">Are you sure?</h4>
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                            </div>

                            <!-- Modal footer -->
                            <div class="modal-footer">
                                <button type="button" class="btn btn-danger"
                                    data-dismiss="modal">Close</button>
                                <a th:href="@{'/home/'+${fgp.fingerprintId}+'/deleteFingerprints'}"><button
                                        type="button" class="btn btn-success">Yes</button></a>
                            </div>
                        </div>
                    </div>
                </div>
            </td>

控制器

@GetMapping("/home/{fingerprintId}/deleteFingerprints")
public String deleteFingerprints(@PathVariable("fingerprintId") int id, Model model) {

    Fingerprints fingerprint = fingerprintRepository.findByFingerprintId(id);

    System.out.println("Fingerprint id: " + fingerprint.getFingerprintId());

    fingerprintRepository.delete(fingerprint);

    return "redirect:/home";
}

2 个答案:

答案 0 :(得分:1)

<tr th:each="record : ${recordList}" >              
    <td>
    <button type="button" class="btn btn-outline-light text-dark" data-toggle="modal" th:attr="data-target='#recordDeleteModal'+${record.id}">Delete</button>

     <!-- Modal -->
        <div class="modal fade" th:attr="id='recordDeleteModal'+${record.id}" tabindex="-1" role="dialog" aria-labelledby="recordDeleteModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="recordDeleteModalLabel">Delete Record</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                Are you sure you want to delete this record?
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
                <a href="newRecordForm.html" class="btn btn-primary" th:attr="href='/record/'+${record.id}+'/deleteRecord'">Yes</a>
              </div>
            </div>
          </div>
        </div>
        <!-- Modal end --> 

    </td>

答案 1 :(得分:0)

我发现错误:我替换为:

th:attr="data-target='#myModal'+${fgp.fingerprintId}">D</button> 

th:attr="id='/home/'+${fgp.fingerprintId}+'/deleteFingerprints'">