如何在Symfony2中的Bootstrap模式弹出窗口中动态显示数据 [英] How to display data dynamically in Bootstrap Modal Popup in Symfony2
问题描述
我想在Modal Popup中显示一个编辑表单,其中包含相应ID的信息。换句话说,我想在链接点击的模态弹出窗口中显示数据库中的动态数据。
I want to display an edit form in Modal Popup with the information of respective ID. In other words i want to display the dynamic data from the database in modal popup on the link click.
到目前为止我尝试了什么:
Twig文件哪个有所有数据的列表:
What i have tried so far: Twig file which have list of all the data:
<table class="table table-striped table-hover table-bordered" style="margin-top:30px;" >
<thead>
<tr>
<th>{{ knp_pagination_sortable(entities, '#', 'a.id') }}</th>
<th {% if entities.isSorted('a.name') %} class="sorted"{% endif %}> {{ knp_pagination_sortable(entities, 'Name', 'a.name') }}</th>
<th class="hidden-480">Full Address</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% set count = '1' %}
{% for entity in entities %}
<tr>
<td>{{ entity.id }}</td>
<td>{{ entity.name }}</td>
<td>{{ entity.address }}</td>
<td>
<a href="#" onclick="editDocument();" data-id="{{ entity.id }}" role="button" data-toggle="modal" class="open-editBox" ><button type="button" class="btn blue">Edit</button></a>
{#<a href="{{ path('venue_edit', { 'id': entity.id }) }}">Edit</a>#}
<a href="#deleteModle" data-id="{{ entity.id }}" role="button" data-toggle="modal"><button type="button" class="btn blue">Delete</button></a>
</td>
{% set count = count + '1' %}
{% endfor %}
</tr>
</tbody>
</table>
动态ID传递的jQuery函数:
jQuery function for the dynamic ID pass:
function editDocument(){
$(document).on("click", ".open-editBox", function () {
var editId = $(this).data('id');
$.ajax({
type: 'GET',
url: editId+"/edit",
//data: {"editId": editId},
success: function(response){
// alert($.get());
$('#editPlayerModel').html(response);
}
});
// alert(editId);
//$(".modal-body #editId").val( editId );
});
}
用于编辑数据和呈现表单的控制器功能:
Controller function to edit the data and render the form:
/**
* Displays a form to edit an existing Venue entity.
*
* @Route("/{id}/edit", name="venue_edit")
* @Method("GET")
* @Template()
*/
public function editAction($id)
{
//print_r($id); exit;
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('JplAdminFunctionBundle:Venue')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Venue entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
edit.html.twig
文件包含编辑表单(我希望此表单显示在模态弹出窗口中):
edit.html.twig
file contains the Edit Form (I want this form to display in the modal popup):
{{ form(edit_form) }}
单击EDIT按钮后,它甚至没有显示任何错误
After clicking on the EDIT button, it displays nothing not even any error
注意:我使用 generate:doctrine:crud
命令执行CRUD操作
NOTE: I have used generate:doctrine:crud
command to do the CRUD operations
我知道我在流程或jQuery函数或控制器代码中的某个地方滞后,但无法确定确切的冲突。
I know i am lagging somewhere in the flow or the jQuery function or the controller Code, but not able to identifying the exact conflict.
帮帮我,比喻
推荐答案
<a href="#" onclick="editDocument();" data-id="{{ entity.id }}" role="button" data-toggle="modal" class="open-editBox" ><button type="button" class="btn blue">Edit</button></a>
在上面的 html
结构中你有 onclick
事件已处理,如果您看到 editDocument
js函数,则:
In the above html
structure you have onclick
event handled and if you see your editDocument
js function then:
function editDocument(){
$(document).on("click", ".open-editBox", function () {
var editId = $(this).data('id');
$.ajax({
type: 'GET',
url: editId+"/edit",
//data: {"editId": editId},
success: function(response){
// alert($.get());
$('#editPlayerModel').html(response);
}
});
// alert(editId);
//$(".modal-body #editId").val( editId );
});
}
你有 $(文件).on('点击'...
这是不必要的。我建议使用上面的任何一个。从结构中删除 onclick
并删除你的函数包装围绕 $(文件).on('点击'...
或对您的功能进行更改,如下所示:
you have $(document).on('click'...
which is unnecessary. I would suggest to use any one of the above. Either remove onclick
from structure and remove your function wrapped around $(document).on('click'...
or make changes to your function as below:
<a href="#" onclick="editDocument(this);" data-id="{{ entity.id }}" role="button" class="open-editBox" ><button type="button" class="btn blue">Edit</button></a>
JS
function editDocument(ctrl){ //pass this ctrl from html
var editId = $(ctrl).data('id');
var res="";//store the obtained result
var success=false; //open modal only if success=true
//url should match your server function so I will assign url as below:
var url="/editAction"; //this is the server function you are calling
var data=JSON.stringify({"id":editId});
$.when( //To execute some other functionality once ajax call is done
$.ajax({
type: 'GET',
url: url,
dataType:'json',//type of data you are returning from server
data: data, //better to pass it with data
success: function(response){
res=response;
success=true;
},
error:function(){
//handle error
},
})).then(function(){
if(success)
{
//assign each values obtained from response which is now
//stored in "res" inside modal element by mapping it to
//necessary controls in modal
$("yourmodalid").modal("show"); //show the modal
}
});
}
如果您使用的是 $(文件)。 on('click'....
然后将其更改如下:
OR if you are using $(document).on('click'....
then change it as below:
HTML
<a href="#" data-id="{{ entity.id }}" role="button" class="open-editBox" ><button type="button" class="btn blue">Edit</button></a>
JS
$(document).on("click", ".open-editBox", function () {
var editId = $(this).data('id'); //get the id with this
var res="";//store the obtained result
var success=false; //open modal only if success=true
//url should match your server function so I will assign url as below:
var url="/editAction"; //this is the server function you are calling
var data=JSON.stringify({"id":editId});
$.when(
$.ajax({ //To execute some other functionality once ajax call is done
type: 'GET',
url: url,
data: data, //better to pass it with data
dataType:'json',//type of data you are returning from server
success: function(response){
res=response;
success=true;
},
error:function(){
//handle error
},
})).then(function(){
if(success)
{
//assign each values obtained from response which is now
//stored in "res" inside modal element by mapping it to
//necessary controls in modal
$("yourmodalid").modal("show"); //show the modal
}
});
});
我觉得你不需要锚内的按钮你可以应用
类来锚定自己以获得如下按钮的感觉:
I feel you don't need button inside anchor and you can just apply classes to anchor itself to get button feeling as below:
<a href="#" data-id="{{ entity.id }}" role="button" class="open-editBox btn blue">EDIT</a>
关注此事。如果您遇到任何问题,请告诉我
Have a eye on this. Let me know if you face any issues
这篇关于如何在Symfony2中的Bootstrap模式弹出窗口中动态显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!