【问题标题】:cakephp implement modal window for editcakephp 实现编辑模式窗口
【发布时间】:2015-03-04 23:27:58
【问题描述】:

我有一个显示记录列表的简单应用程序,我想实现一个引导模式窗口来编辑记录。我是 php 新手,但我正在尝试 cakePHP,因为我更喜欢 MVC。

我在每个表格行上都有一个下拉菜单,选择后,我想打开模式,进行更改并保存。目前,我正在实现一个链接来调用 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>

这可能是错误的实施方式,因此我愿意接受建议。任何帮助表示赞赏。

谢谢

【问题讨论】:

    标签: jquery twitter-bootstrap cakephp


    【解决方案1】:

    这就是我最终要做的。该解决方案是从 stackoverflow 上的其他答案拼凑而成的:

    在控制器中,我添加了对 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 调用,并将结果添加到模态框的正文中。

    希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

      将以下代码添加到您的 UnitsController beforeFilter 回调方法中:

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

      这会使用 CakeRequest 对象的 is() 方法并传递 ajax 值来检查请求是通过 Ajax 发出的。如果是这种情况,我们将禁用缓存,因为我们总是希望为我们的 Ajax 调用提供新鲜的服务。这应该解决您遇到的问题,即“数据正在被缓存,因此即使在选择新记录后,它也会显示来自显示的第一条记录的数据。”

      【讨论】:

        猜你喜欢
        • 2015-07-16
        • 1970-01-01
        • 1970-01-01
        • 2015-07-25
        • 1970-01-01
        • 1970-01-01
        • 2023-01-23
        • 1970-01-01
        相关资源
        最近更新 更多