是否可以在bootstrap modal中显示视图(例如add.ctp)?如果可以的话,只能在add.ctp文件中呈现html而不加载默认布局?
因为目前我正在尝试构建一个类似于add.ctp文件中的自定义表单,以便在模态中显示它,并且它真的很痛苦,试图发布并获取json对象以便提交表单并填充我申请中的网格。
是的,这是可能的。
在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"));
});