【问题标题】:How to use zend delete action in confirmation popup?如何在确认弹出窗口中使用 zend 删除操作?
【发布时间】:2014-03-26 07:24:46
【问题描述】:

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

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 视图,我想要的是在我确认弹出窗口时删除该项目。

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

【问题讨论】:

  • 这个问题的答案正如@emaillenin 提到的那样,document.form.submit(); 行被删除并在$(this).dialog( "close" ); 之后添加window.location.href = ....

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我知道这是一个老问题,但它对我有帮助。所以我想肯定还有其他人也在寻找答案。所以这里我是怎么做的..

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

    2. 下载 javascript 并将其保存到您的 'js' 文件夹中(我放在 'js/azza/deletebox.js' 中)

    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. 将'id'和'class'都设置为“dialog-confirm”以触发javascript函数非常重要

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

    就是这样……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多