Issue
I want to delete the object from table. The problem is that I do not delete the object I click. I always delete the object above. what am I doing wrong?
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">×</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>
Controller
@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";
}
Solution
I found the error: I replaced with:
th:attr="data-target='#myModal'+${fgp.fingerprintId}">D</button>
and with
th:attr="id='/home/'+${fgp.fingerprintId}+'/deleteFingerprints'">
Answered By - abc
Answer Checked By - Pedro (JavaFixing Volunteer)