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

标签 javascript php jquery twitter-bootstrap symfony

我在我的 Symfony 项目中遇到了模态问题。目前,我在表中有一个成员列表。对于每一行,都有一个用于操作的按钮:查看、编辑和删除。我在我的 Twig View 中这样做:

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

如您所见,链接是动态的,带有记录的 ID。现在,我想为所选操作打开一个模式。在我的示例中,我需要转到类似/member/edit/IdOfUser

如何在模式中加载此 View ?我是否需要为此创建一个表单模板?我想我需要使用 ajax 来加载动态 View 。

最佳答案

我建议在可以使用属性 data-XXX 时使用 bootstrap 3 的模态(这里的例子是 data-whatever)

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

$('#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)
})

关于javascript - 使用 Symfony 打开具有动态值的模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32317824/

相关文章:

c# - 如何将我的 C# 代码转换为 js 方法替换

javascript - 无法将 JWT header 设置为 axios

javascript - 函数中的拼接不会按预期更改字符

PHP opendir 和 readdir 与数组目录(不是一个)

javascript - 自定义用户 HTML 输入安全

php - 如何检查多维数组中的任何值是否为空?

javascript - Ajax 通过 post 将文本从一个文本区域复制到另一个文本区域

javascript - 将 cookie 中的值存储到 JavaScript 变量

javascript - 如何防止计时器在浏览器刷新时重置?

jquery - 单击切换器图标后,Bootstrap 下拉菜单消失