引導程序 3 確認刪除 cakephp 中的模式
2023-03-19
php問題
html5模板網
bootstrap 3 confirm delete modal in cakephp(引導程序 3 確認刪除 cakephp 中的模式)
本文介紹了引導程序 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'));
});
這篇關于引導程序 3 確認刪除 cakephp 中的模式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!