Access the object of a specific row in a Bootstrap Table and pass into Modal

问题: 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 fr...

问题:

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();

            }
        });
    });

回答1:

Actually I do not understand the question, but I suggest that you can either create a modal for each row element while rendering template or write a custom JS script and pass object ID via data attribute and store it into some hidden field.

  • 发表于 2019-02-15 15:55
  • 阅读 ( 207 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除