2014-01-17 32 views
1

我有一個簡單的應用程序,顯示記錄列表,我想實現一個引導模態窗口來編輯記錄。我是新來的PHP,但我嘗試cakePHP,因爲我更喜歡MVC。cakephp實現模式編輯窗口

我在每個表格行上都有一個下拉列表,選擇後,我想要打開模式,進行更改並保存。目前,我正在實現一個鏈接來調用url來獲取異步數據,並用響應更新div。我有這個工作,但數據正在緩存,所以即使在選擇新記錄後,它也顯示第一條記錄的數據。我確信我可以找到一種方法來清除它,但這讓我質疑我是如何執行模式窗口的。

列表視圖有以下幾點:

<li> 
      <?php    
      echo $this->Js->link("Edit settings","/units/edit/".$unit['Unit']['id'] , 
        array('update' => '#editModal', 
         'htmlAttributes' => array(
          'data-toggle' => 'modal', 
          'data-target' => '#editModal' 
        ))); 
      ?> 
     </li> 

控制器:

if (!$this->Unit->exists($id)) { 
    throw new NotFoundException(__('Invalid unit')); 
} 
if ($this->request->is(array('post', 'put'))) { 
    if ($this->Unit->save($this->request->data)) { 
    $this->Session->setFlash(__('The unit has been saved.')); 
    return $this->redirect(array('action' => 'index')); 
    } else { 
    $this->Session->setFlash(__('The unit could not be saved. Please, try again.')); 
    } 
} else { 
if ($this->request->is('ajax')) { 
    $this->render('edit', 'ajax'); 
    } 
} 

編輯觀點:

<div class="modal-dialog"> 

× 莫代爾標題

<?php 

    echo $this->Form->create('Unit'); 
    echo $this->Form->input('name', array('class' => 'form-control', 'div' => 'form-group', 'disabled' => 'disabled')); 
    echo $this->Form->input('internet_class_id', array('class' => 'form-control', 'div' => 'form-group')); 
    $offOn = array(0 => 'Off', 1 => 'On'); 
    echo $this->Form->input('water_setting_id', array('options' => $offOn, 'default' => 0, 
     'class' => 'form-control', 'div' => 'form-group')); 
    $amenitySettings = array(0 => 'Deny', 1 => 'Allow'); 
    echo $this->Form->input('amenity_setting_id', array('options' => $amenitySettings, 'default' => 0, 
     'class' => 'form-control', 'div' => 'form-group')); 
    echo $this->Form->input('cable_setting_id', array('options' => $offOn, 'default' => 0, 
     'class' => 'form-control', 'div' => 'form-group')); 
    echo $this->Form->hidden('id'); 
    echo $this->Form->hidden('building_id'); 
    echo $this->Form->button('Save', array('type' => 'submit', 'class' => 'btn btn-primary')); 
    echo $this->Form->end(); 
    ?> 

</div> 
<div class="modal-footer"> 
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
    <button type="button" class="btn btn-primary">Save changes</button> 
</div> 

這可能是實現,所以我願意接受建議走錯了路。任何幫助表示讚賞。

謝謝

回答

2

這是我最終做的。這種解決方案是從#1中發現的其它答案拼湊在一起:

在控制器中,我添加一個簡單的檢查用於AJAX請求,並且如果是這樣,用空AJAX佈局呈現的視圖:

if ($this->request->is('ajax')) { 
    $this->render('edit', 'ajax'); 
    } 

我加入這個jQuery的觀點:

$("a[data-target=#flexModal]").click(function(ev) { 
ev.preventDefault(); 
var target = $(this).attr("href"); 
// load the url and show modal on success 
$("#flexModal .modal-body").load(target, function() { 
    $("#flexModal").modal("show"); 
}); 
}); 

利用這一點,每次我點擊我有,一個Ajax調用,然後將結果添加到模態的體工作模式的管理按鈕。

希望這可以幫助別人。

0

下面的代碼添加到你的UnitsController beforeFilter回調方法:

public function beforeFilter() { 
      parent::beforeFilter(); 
      if ($this->request->is('ajax')) { 
        $this->response->disableCache(); 
      } 
} 

這將檢查該請求被通過Ajax使用CakeRequest對象的IS()方法並傳遞AJAX值製成。如果是這種情況,我們將禁用緩存,因爲我們總是希望爲我們的新Ajax調用提供服務。這應該處理您所處的問題,「數據正在被緩存,因此即使在選擇新記錄後,它也會顯示來自所顯示的第一條記錄的數據。」