2010-07-27 125 views
0

我創建了一個刪除項目的確認頁面。這工作完美。Zend Framework帶有ajax和發佈值的fancybox確認對話框

現在我想在fancybox中顯示確認頁面,仍然傳遞所有變量並刪除數據或關閉對話框。

重要的是,該過程仍然可以像現在一樣工作(delete-> confirmationpage-> redirect/deletion),以便禁用javascript的用戶可以毫無問題地執行這些操作。

現在我一直在閱讀關於zend框架,ajax,json和更多內容,但是我閱讀得越多,我理解的越少。

所以,總之,我的問題:

我想一個變量傳遞給的fancybox,如果「是」執行刪除操作或「否」關閉對話框。這在Zend框架和jQuery中。

任何意見或提示,非常感謝!

+0

我通過在iframe中打開確認頁面解決了問題。不幸的是,我現在沒有時間更多地關注這個問題,所以我會回來的! – 2010-08-16 14:30:58

回答

0

整個問題就來了今天想我可以給它一個新面貌。對於Zend框架的解決方案是非常簡單的:

這是操作的樣子:

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" 
      } 
     }); 
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。設置一些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 
}