简体   繁体   English

使用Symfony打开具有动态值的模态

[英]Open a modal with dynamic value with Symfony

I am facing an issue with a modal in my Symfony project. 我在Symfony项目中遇到模态问题。 For the moment, I have a list of member in a table. 目前,我在表中有一个成员列表。 For every row, there is a button for actions : view, edit and delete. 每行都有一个用于执行操作的按钮:查看,编辑和删除。 I am doing this in my twig view : 我正在树枝视图中执行此操作:

 <a href="{{ path('member_edit', {'id': member.id})}}" class="btn btn-default">
                                            <i class="glyphicon glyphicon-info-sign"></i>
                                        </a>

As you can see, the link is dynamic with the ID of the recod. 如您所见,该链接是动态的,具有Recod的ID。 Now, I want to open a modal for the choosen action. 现在,我想为所选动作打开一个模式。 In my example, I need to go to something like /member/edit/IdOfUser 在我的示例中,我需要转到/ member / edit / IdOfUser之类的内容

How I can load this view in a modal ? 如何在模态中加载此视图? Do I need to create a form template for doing that ? 我需要为此创建一个表单模板吗? I think I need to use ajax for loading the dynamic view. 我想我需要使用ajax来加载动态视图。

I suggest using modal of bootstrap 3 when you can use the attribute data-XXX (exemple here data-whatever) 我建议您在可以使用属性data-XXX(此处为data-whatever)时使用引导程序3的模式

HTML HTML

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="{{ member.id }}">
    <i class="glyphicon glyphicon-info-sign"></i>
</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Demo</h4>
            </div>
            <div class="modal-body">
                Body modal
                <input type="text" name="id" class="modal-body input"/>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

Javascript 使用Javascript

$('#exampleModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) // Button that triggered the modal
  var id= button.data('whatever') // Extract info from data-* attributes
  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
  var modal = $(this)
  modal.find('.modal-title').text('The ID is: ' + id)
  modal.find('.modal-body input').val(id)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM