代码之家  ›  专栏  ›  技术社区  ›  Rick de Graaf

Zend Framework fancybox与ajax的确认对话框和发布的值

  •  0
  • Rick de Graaf  · 技术社区  · 15 年前

    我创建了一个删除项目的确认页。这很管用。

    现在我想在fancybox中显示确认页面,仍然传递所有变量并删除数据或关闭对话框。

    简而言之,我的问题是:

    2 回复  |  直到 15 年前
        1
  •  1
  •   Ashley    15 年前

    您需要在ajax中使用内容切换,这样可以适当地呈现您的操作,例如:

    function confirmDeleteAction() {
      if($this->_request->isXmlHttpRequest()) {
        //This is ajax so we want to disable the layout
        $this->_helper->layout->disableLayout();
        //Think about whether you want to use a different view here using: viewRenderer('yourview')
      }
      //The normal code can go here
    }
    
    function deleteAction() {
      //You probably don't want to show anything here, just do the delete logic and redirect therefore you don't need to worry where it's coming from
    }
    

    在您的fancybox中,有一个包含一个窗体和两个按钮的视图,窗体应该指向您的delete操作,并将您删除的内容的id作为get参数。设置一些javascript,比如(这是mootools代码,但您可以轻松地转换它):

    $('confirmButton').addEvent('click', function(e) {
       e.preventDefault();
       this.getParent('form').submit();
    }
    $('cancelButton').addEvent('click', function(e) {
       e.preventDefault();
       $('fancyBox').destroy(); //I'm not sure it has an id of fancyBox, never used it
    }
    
        2
  •  0
  •   Rick de Graaf    14 年前

    public function deleteAction()
        {
            $this->_helper->layout()->disableLayout();
    
            $this->view->news = $this->newsService->GetNews($this->_getParam('id'));
    
            if($this->getRequest()->isPost())
            {
                if($this->getRequest()->getPost('delete') == 'Yes')
                {
                    $this->newsService->DeleteNews($this->_getParam('id'), $this->view->user->username, $this->view->translate('deleted: ').'<strong>'.$this->view->pages[0]['title'].'</strong>');
                    $this->_helper->flashMessenger(array('message' => $this->view->translate('The page is deleted'), 'status' => 'success'));
                    $this->_helper->redirectToIndex();
                }
                elseif($this->getRequest()->getPost('delete') == 'No')
                {
                    $this->_helper->flashMessenger(array('message' => $this->view->translate('The page is <u>not</u> deleted'), 'status' => 'notice'));
                    $this->_helper->redirectToIndex();
                }
            }
        }
    

    <div>
    <h2><?php echo $this->translate('Delete news'); ?></h2>
    <?php
    foreach($this->news as $news)
    {
    ?>
    <p>
        <?php echo $this->translate('You are now deleting <strong>\'').$news['title'].$this->translate('\'</strong>. Are you sure about this?'); ?>
    </p>
    <p>
        <?php echo $this->translate('<strong>Note! </strong>This action is inreversable, even for us!'); ?>
    </p>
    <form action="<?php echo $this->baseUrl(false).'/news/index/delete/'.$news['id']; ?>" method="post">
    <?php
    }
    ?>
        <input class="submit deleteYes" type="submit" name="delete" value="<?php echo $this->translate('Yes'); ?>" />
        <input class="submit deleteNo" type="submit" name="delete" value="<?php echo $this->translate('No'); ?>" />
    </form>
    

    这就是删除文件的链接的外观(在带有我的数据库结果的foreach循环中)

    <a class="deleteConfirmation" href="<?php echo $this->baseUrl(false).'/news/index/delete/'.$news->id; ?>">delete</a>
    

    这就像你所期望的那样;单击“删除”时,用户将转到“删除确认”页,并在表单提交后将用户重定向回索引。但我希望在对话框中确认(在我的例子中,我使用fancybox)。为此,请将以下jquery添加到索引中:

    $('.deleteConfirmation').fancybox({
                // Normal fancybox parameters
                ajax : {
                    type    : "POST"
                }
            });