gpt4 book ai didi

javascript - 如何在 Symfony2 的 Bootstrap 模态弹出窗口中动态显示数据

转载 作者:行者123 更新时间:2023-12-03 09:30:15 25 4
gpt4 key购买 nike

我想在模态弹出窗口中显示一个编辑表单,其中包含相应 ID 的信息。换句话说,我想在链接单击时以模式弹出窗口显示数据库中的动态数据。

到目前为止我已经尝试过:Twig 文件包含所有数据的列表:

<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 函数:

 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 函数:

   /**
* 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 文件包含编辑表单(我希望此表单显示在模式弹出窗口中):

{{ form(edit_form) }}

单击“编辑”按钮后,它不显示任何内容,甚至没有任何错误

注意:我使用了generate:doctrine:crud命令来执行CRUD操作

我知道我在流程、jQuery 函数或 Controller 代码中的某个地方滞后,但无法识别确切的冲突。

帮帮我,谢谢

最佳答案

<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 函数,则:

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

您有 $(document).on('click'... ,这是不必要的。我建议使用上述任何一项。要么删除 onclick从结构中删除包含 $(document).on('click'... 的函数,或对函数进行如下更改:

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

或者,如果您使用的是 $(document).on('click'....,则按如下方式进行更改:

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

关于javascript - 如何在 Symfony2 的 Bootstrap 模态弹出窗口中动态显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31532805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com