2014-01-17 31 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调用提供服务。这应该处理您所处的问题,“数据正在被缓存,因此即使在选择新记录后,它也会显示来自所显示的第一条记录的数据。”