0
我正在使用CakePHP 3.3,我有一個users
的列表,並且我想在確認取消特定用戶的刪除之前顯示SweetAlert確認框。CakePHP 3.3使用SweetAlert確認刪除記錄
這是src/Template/Users/index.ctp
文件片段包含了用戶的列表:
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= $this->Html->image('../'.$user->avatar, ['class' => 'img-avatar']); ?></td>
<td><?= h($user->username) ?></td>
<td><?= h($user->role) ?></td>
<td><?= h($user->created) ?></td>
<td><?= h($user->modified) ?></td>
<td class="actions">
<span class="label label-default"><?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?></span>
<span class="label label-default"><?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?></span>
<span class="label label-default"><?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete # {0}?', $user->id)]) ?></span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
<button class="btn-del">DUMMY BUTTON</button>
然後,我有一個腳本我src/Template/Layout/default.ctp
:
<script>
document.querySelector('.btn-del').onclick = function(){
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
},
function(){
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});
};
</script>
我試過的腳本,它的工作使用Dummy Button
,但我不知道如何使它工作,因此它可以確認或取消user
記錄的刪除,並在每次刪除後重定向到index.ctp
。
對不起,我是CakePHP和JS的初學者&謝謝您提前。