我想使用确认弹出窗口来删除我的项目,所以这是我的删除操作:
public function deleteAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
$article = $this->getObjectManager()->find('ApplicationEntityArticle', $id);
if ($this->zfcUserAuthentication()->hasIdentity()) {
if ($this->request->isPost())
{
$this->getObjectManager()->remove($article);
$this->getObjectManager()->flush();
return $this->redirect()->toRoute('blog');
}
}
else
{
return $this->redirect()->toRoute('user');
}
return new ViewModel(array('article' => $article));
}
这是我的博客视图,我有删除链接:
<a href="<?php echo $this->url('delete', array('action'=>'delete', 'id' => $articles->getId())) ?>" class="btn btn-default btn-lg" onclick="if (confirm('Are you sure?')) { document.location = this.href; } return false;" id="dialog">Delete Article</a>
<?php endif;?>
<script type="text/javascript">
$(function() {
$( "#dialog:ui-dialog" ).dialog( "destroy" );
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Are you sure": function() {
document.form.submit();
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
</script>
问题是当我按下链接时,它会重定向到 delete.phtml 视图,我想要的是在确认弹出窗口时删除该项目。
因此,如果有人有任何解决方案,我将不胜感激:)
confirm
方法。
if (window.confirm('Are you sure you want to delete?'))
{
window.location = "<?php echo $this->url('delete', array('action'=>'delete', 'id' => $articles->getId())) ?>"
}
else
{
// do nothing
}
如果要使用 jQuery UI 对话框,
buttons: {
"Are you sure": function() {
window.location = "<?php echo $this->url('delete', array('action'=>'delete', 'id' => $articles->getId())) ?>"
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
我知道这是一个老问题,但它对我有帮助。 所以我想肯定还有其他人在寻找答案。 所以在这里我是如何做到的..
-
转到 http://adriancallaghan.co.uk/jquery-confirmation-box-thickbox/
-
下载 JavaScript 并将其保存到您的"js"文件夹中(我的我放在"js/azza/deletebox.js"中)
-
然后在视图 *.phtml 文件中添加指向该文件的链接
$this->inlineScript()->prependFile($this->basePath('js/azza/deletebox.js'));
-
然后按如下方式编辑您的 HREF
<a href="<?php echo $this->url('article', array('action' => 'delete', 'id' => $article->getId())); ?>" title="<?php echo "Delete ".$article->getNama(); ?>" id="dialog-confirm" class="dialog-confirm">Delete</a>
-
将"id"和"class"设置为"dialog-confirm"以触发JavaScript函数非常重要
-
你还需要Jquery和Jquery-UI才能使这个脚本工作。
就是这样。。。