I have a table where each row is an object and shows certain properties. Each row also has a button which opens a deletion modal which will delete the object on that row from the database. The modal Twig template has access to the array of objects displayed, but I need a way of finding which object to search the DB for and delete.
Here is the twig template code to display the data.
{% for announcement in announcements %}
<tr>
<th scope="row">{{ announcement.content }}</th>
<td>{{ announcement.isDisplayed() }}</td>
<td style="float:right;">
<div>
<button class="btn btn-sm" data-toggle="modal" data-target="#announcementDeleteModal">
<span class="fa fa-trash btn-link"></span>
Delete announcement
</button>
The modal template, which also has accents to the array announcements
<div class="modal fade" id="announcementDeleteModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">
<span class="fa fa-cat"></span><div>Srsly delete that?</div>
</h5>
</div>
<div class="modal-body">
{{ form_start(deleteForm) }}
{{ form_end(deleteForm) }}
<span class="fa fa-cat"></span><div>Srsly delete that?</div>
<button type="button" class="btn btn-secondary" data-dismiss="modal" aria-label="Close">
Fine. Keep it.
</button>
</div>
</div>
</div>
Here is the jQuery code which sends the ajax request. The var id
is capturing the correct data. I just need to access it via the controller. I tried $request->get('id')
and $request->get('id')
,
$('.action-button').click(function () {
alert("ran");
var id = $(this).data('id');
alert(id);
$.ajax({
url:"/admin/announcements",
method: "POST",
cache: false,
data: { id: id },
success: function (html) {
alert("success");
// $(this).parent().parent().remove();
}
});
});