如何在确认弹出窗口中使用zend delete动作?

时间:2014-02-24 14:47:03

标签: php jquery zend-framework doctrine-orm zend-framework2

我想使用确认弹出窗口删除我的项目,因此这是我的删除操作:

public function deleteAction()
{       
    $id = (int) $this->params()->fromRoute('id', 0);
    $article = $this->getObjectManager()->find('\Application\Entity\Article', $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视图,我想要的是在确认弹出窗口时删除该项目。

所以,如果有人有任何解决方案,我将非常感激:)

2 个答案:

答案 0 :(得分:2)

您可以使用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" );
                            }
                        }

答案 1 :(得分:0)

我知道这是一个老问题,但它对我很有帮助。所以我认为必须有其他人也在寻找答案。所以我在这里怎么做..

  1. 转到http://adriancallaghan.co.uk/jquery-confirmation-box-thickbox/

  2. 下载并将javascript保存到您的&#39; js&#39;文件夹(我放入&#39; js / azza / deletebox.js&#39;)

  3. 然后在视图* .phtml文件中添加指向该文件的链接

    $this->inlineScript()->prependFile($this->basePath('js/azza/deletebox.js'));
    
  4. 然后按如下方式编辑你的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>
    
  5. 设置&#39; id&#39;非常重要和&#39;班级&#39; as&#34; dialog-confirm&#34;触发javascript函数

  6. 您还需要Jquery和Jquery-UI才能使此脚本正常工作

  7. 那是......