CakePHP 3.6.14:在引导模式中渲染视图(.ctp)

问题描述 投票:1回答:1

是否可以在bootstrap modal中显示视图(例如add.ctp)?如果可以的话,只能在add.ctp文件中呈现html而不加载默认布局?

因为目前我正在尝试构建一个类似于add.ctp文件中的自定义表单,以便在模态中显示它,并且它真的很痛苦,试图发布并获取json对象以便提交表单并填充我申请中的网格。

php cakephp bootstrap-modal
1个回答
1
投票

是的,这是可能的。

在Ajax文件夹中创建add.ctp,例如:

/Posts
  index.ctp
  /Ajax
  add.ctp

在Postings :: add()中设置Ajax布局并使用js get / posts / add和render modal。

读:

https://book.cakephp.org/3.0/en/controllers/components/request-handling.html https://book.cakephp.org/3.0/en/views.html#layouts

编辑:

在控制器中

public function add()
{
   // your code here ...
   if ($this->getRequest()->is('ajax')) {
       // render "add" view in Ajax folder and use "ajax" Layout
       $this->render('Ajax/add', 'ajax')
   }
}

https://book.cakephp.org/3.0/en/controllers.html#rendering-a-specific-template

编辑2(jQuery部分)示例

<button type="button" data-toggle="modal" data-remote="<= $this->Url->build(/* ADD HERE YOUR PARAMS*/) 
 ?>" data-target="#myModel">Open Model</button>

$('body').on('click', '[data-toggle="modal"]', function(){
        $($(this).data("target")+' .modal-body').load($(this).data("remote"));
    });  
© www.soinside.com 2019 - 2024. All rights reserved.