引导程序 3 确认删除 cakephp 中的模式
我有一个记录表,其中每一行都有一个删除链接.您会找到用于删除操作的 cakephp :
公共函数删除($id){如果 ($this->request->is('get')) {抛出新的 MethodNotAllowedException();}如果 ($this->Category->delete($id)) {$this->Session->setFlash('Votre élément a été supprimé.','default',array(),'success');返回 $this->redirect(array('action' =>'index'));}}
因此,当我单击删除按钮时,会显示一个原始 javascript 确认对话框以确认视图中的删除操作.这是一个包含删除链接的 index.ctp :
<table class="table table-striped table-condensed table-bordered"><tr><th>title</th><th>动作</th></tr><?php foreach ($categorys as $category): ?><tr><td><?php echo $category['Category']['title'];?></td><td><?phpecho $this->Html->link('View',数组('控制器' => '类别', '动作' => '视图', $category['Category']['id']),数组('class' => 'btn btn-info btn-sm active'));?><?phpecho $this->Html->link('编辑', array('action' => 'edit', $category['Category']['id']),数组('class' => 'btn btn-primary btn-sm active'));?><?phpecho $this->Form->postLink('删除',array('action' => 'delete', $category['Category']['id']),array('confirm' => '你真的要删除这个元素吗?','class' => 'btn btn-danger btn-sm active'));?></td></tr><?php endforeach;?><?php unset($category);?>
所以对于我想要的后链接,当我点击链接时,它会向我显示一个引导程序确认模式,如下所示:
<div class="modalfade" 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">类别删除</h4><div class="modal-body">你真的要删除这个元素吗?
<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">关闭</button><a class="btn btn-danger 危险">确认</a>
谁能帮我使用cake php的jshelper来创建一个引导模式对话框而不是默认的对话框.
谢谢.
解决方案我编辑我的答案并改进代码
在您的索引页面上而不是 postLink,创建一个按钮或链接来调用模态,即
Html->link($this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-trash')),'#',大批('class'='btn btn-danger btn-confirm','数据切换'=>'模态','数据目标' =>'#确认删除','数据动作'=>路由器::网址(数组('action'=>'delete',$category['Category']['id'])),'逃脱' =>错误的),错误的);?>
在你的模态中添加 postLink 没有确认消息,而不是把消息放在 false:
Form->postLink('确认',数组('动作' => '删除'),array('class' => 'btn btn-danger btn-sm active'),错误的,));?>
在bootstrap.js之后添加这段js代码
$(document).ready(function() {$(".btn-confirm").on("click", function () {var action = $(this).attr('data-action');$("form").attr('action',action);});});
或者按照user1655410的建议添加这个js代码
$('#ConfirmDelete').on('show.bs.modal', function(e) {$(this).find('form').attr('action', $(e.relatedTarget).data('action'));});
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'));
}
}
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>
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>
can someone help me to use the jshelper of the cake php to create a bootstrap modal dialog instead of the default one.
Thank you.
解决方案I edit my answer and improve code
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);
?>
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,
)
);
?>
add this js code after bootstrap.js
$(document).ready(function() {
$(".btn-confirm").on("click", function () {
var action = $(this).attr('data-action');
$("form").attr('action',action);
});
});
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'));
});
相关文章