bootstrap 3确认删除模式在cakephp [英] bootstrap 3 confirm delete modal in cakephp
问题描述
我有一个记录表,其中有一个删除链接的每一行。你会发现cakephp的删除操作:
hi i have a table of records where there's a delete link for every row.Her you will find cakephp for the delete action :
public function delete($id){
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Category->delete($id)) {
$this->Session->setFlash( 'Votre élément a été supprimé.','default',array(),'success');
return $this->redirect(array('action' => 'index'));
}
}
所以当我点击删除按钮,生成的javascript确认对话框被显示,以确认删除在视图中的操作。这里是包含删除链接的index.ctp:
so when i click on the delete button a raw javascript confirm dialog box is diplayed to confirm the action of the deletion in the view. here's an index.ctp containing the delete link :
<!--table content-->
<table class="table table-striped table-condensed table-bordered">
<tr>
<th>title</th>
<th>Actions</th>
</tr>
<?php foreach ($categorys as $category): ?>
<tr>
<td><?php echo $category['Category']['title']; ?></td>
<td>
<?php
echo $this->Html->link('View',
array('controller' => 'categories', 'action' => 'view', $category['Category']['id']),
array('class' => 'btn btn-info btn-sm active')
); ?>
<?php
echo $this->Html->link(
'Edit', array('action' => 'edit', $category['Category']['id']),
array('class' => 'btn btn-primary btn-sm active')
);
?>
<?php
echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $category['Category']['id']),
array('confirm' => 'Do you want really to delete thi element?','class' => 'btn btn-danger btn-sm active')
);
?>
</td>
</tr>
<?php endforeach; ?>
<?php unset($category); ?>
</table>
所以对于postlink我想当我点击链接,它会显示我一个引导确认模态喜欢这:
so for the postlink i want when i click on the link it will show me a bootstrap confirmation modal like this :
<!-- Modal -->
<div class="modal fade" id="ConfirmDelete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Category deletion</h4>
</div>
<div class="modal-body">
Do you really want to delete thi element?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<a class="btn btn-danger danger">Confirm</a>
</div>
</div>
</div>
</div>
有人可以帮助我使用蛋糕php的jshelper创建一个引导模式对话框,而不是默认一个。
can someone help me to use the jshelper of the cake php to create a bootstrap modal dialog instead of the default one.
谢谢。
推荐答案
编辑我的答案并改进代码。
在您的索引页上,而不是postLink,创建一个按钮或链接,
$ b
On your index page instead postLink, create a button or link that will call the modal, ie
<?php
echo $this->Html->link(
$this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-trash')),
'#',
array(
'class'=>'btn btn-danger btn-confirm',
'data-toggle'=> 'modal',
'data-target' => '#ConfirmDelete',
'data-action'=> Router::url(
array('action'=>'delete',$category['Category']['id'])
),
'escape' => false),
false);
?>
在您的模式中添加postLink而不确认消息,而不是消息put false:
In your modal add postLink without confirmation message, instead of the message put false:
<?php
echo $this->Form->postLink(
'Confirm',
array('action' => 'delete'),
array('class' => 'btn btn-danger btn-sm active'),
false,
)
);
?>
在bootstrap.js 之后添加此js代码
$(document).ready(function() {
$(".btn-confirm").on("click", function () {
var action = $(this).attr('data-action');
$("form").attr('action',action);
});
});
或按照用户建议添加此js代码 b
$ b
or as suggested by user1655410 add this js code
$('#ConfirmDelete').on('show.bs.modal', function(e) {
$(this).find('form').attr('action', $(e.relatedTarget).data('action'));
});
这篇关于bootstrap 3确认删除模式在cakephp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!