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

时间:2010-07-27 15:28:19

标签: ajax zend-framework fancybox

我创建了一个删除项目的确认页面。这非常有效。

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

重要的是,该过程仍然可以像现在一样工作(删除 - >确认页面 - >重定向/删除),以便禁用javascript的用户可以毫无问题地执行这些操作。

现在我已经阅读了关于zend框架,ajax,json等等,但是我读的越多,我理解的就越少。

简而言之,我的问题是:

我想将变量传递给fancybox,如果'yes'执行删除操作,或者'no'关闭对话框。这在zend框架和jquery中。

非常感谢任何建议或提示!

2 个答案:

答案 0 :(得分:1)

您需要在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中,有一个带有表单和两个按钮的视图,表单应该指向你的删除操作,其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
}

答案 1 :(得分:0)

今天遇到了这个问题,并认为我可以给它一个新的面貌。对于Zend Framework,解决方案非常简单:

这是行动的表现:

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();
            }
        }
    }

delete.phtml

<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"
            }
        });