CAKEPHP3使用引导模式确认删除条目?
我正在尝试使用引导模式来确认删除操作。我正在使用CakePHP3,而且是新手 到目前为止,我能够创造这么多 如果我可以成功地替换为单击的按钮的id,那么问题将得到解决 UsersController.phpCAKEPHP3使用引导模式确认删除条目?,php,jquery,twitter-bootstrap,cakephp-3.x,Php,Jquery,Twitter Bootstrap,Cakephp 3.x,我正在尝试使用引导模式来确认删除操作。我正在使用CakePHP3,而且是新手 到目前为止,我能够创造这么多 如果我可以成功地替换为单击的按钮的id,那么问题将得到解决 UsersController.php public function delete($id) { $this->request->allowMethod(['post', 'delete']); $user= $this->Users->get($id); if ($this-&
public function delete($id)
{
$this->request->allowMethod(['post', 'delete']);
$user= $this->Users->get($id);
if ($this->Users->delete($user)) {
$this->Flash->success(__('The user with id: {0} has been deleted.', h($id)));
return $this->redirect(['action' => 'index']);
}
}
index.ctp
<?= $this->Html->tag('i','',['class' => 'fa fa-times fa-fw icon-delete deleteUser', 'data-toggle' => 'modal', 'data-target' => '#confirmModal' , 'id' => $user->user_id ]) ?>
<!-- Modal -->
<div class="modal fade" id="confirmModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">Are you sure?</div>
<div class="modal-footer">
<?= $this->Form->postLink(
$this->Html->tag('button','Delete',['class' => 'btn btn-default pull-right']),
['action' => 'delete', <id_here>],
['escape' => false])
?>
<button type="button" class="btn btn-default pull-right" data-dismiss="modal">Cancel</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
你确定吗?
取消
模式中删除按钮部件的生成标记为
<form action="/boot/users/delete/ABCD20090004" name="post_56e141b0297cc915184088" style="display:none;" method="post"><input name="_method" value="POST"></form>
<a href="#" onclick="document.post_56e141b0297cc915184088.submit(); event.returnValue = false; return false;">
<button class="btn btn-default pull-right">Delete</button>
</a>
试试:
注意:列表(循环)中的每个元素都需要一个模式。通过模式上的删除按钮传递要删除的记录的用户id,而不是单击以显示模式的删除按钮。修改index.ctp,如下所示 此时不要传递用户id
<?= $this->Html->tag('i','',['class' => 'fa fa-times fa-fw icon-delete deleteUser', 'data-toggle' => 'modal', 'data-target' => '#confirmModal']) ?>
在下面的删除按钮上传递id
<!-- Modal -->
<div class="modal fade" id="confirmModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">Are you sure?</div>
<div class="modal-footer">
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['class' => 'btn btn-default pull-right']); ?>
<button type="button" class="btn btn-default pull-right" data-dismiss="modal">Cancel</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
你确定吗?
取消
另外,不要忘记初始化模式
<script>
$(document).ready(function () {
$('#confirmModal').modal('show');
});
</script>
$(文档).ready(函数(){
$('confirmModal').modal('show');
});
您如何将id传递给删除按钮?@madalinivascu这是我的问题。我需要想出一个办法。如果这样做,问题将得到解决。行是动态生成的,因此,如果我使用上述方法,无论单击哪个删除按钮,最后一行都将被删除。粘贴我添加的问题中生成的模式。这是使用您的方法的输出是的,如果我为每行数据动态创建模型,您的答案将有效。但是我不希望这样,你需要一些ajax来完成你正在尝试的工作
<script>
$(document).ready(function () {
$('#confirmModal').modal('show');
});
</script>