php - Yii2 - 使用 Ajax 以模态保存表单
问题描述
我有两种型号,一种是“注册产品”(我有一个包含所有品牌的下拉菜单),另一种是“品牌”。我需要在“注册产品”表单中渲染表单“品牌”以使用引导Modal
寡妇创建一个新品牌并且它可以工作,但我需要新创建的品牌在表单的下拉列表中可用,register-products
这样我就不会每次都要刷新页面。
此时发生的情况是,在创建新品牌后,正在重定向到包含新品牌细节的视图。
注册产品表格
<?php
Modal::begin([
'header' => 'Test',
'id' => 'modal',
'size' => 'modal-lg',
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
Html::a('+', ['/brands/create'], ['id' => 'open_modal', 'class' => 'btn btn-success']);
js
$(function(){
$('#open_modal').click(function (){
$.get($(this).attr('href'), function(data) {
$('#modal').modal('show').find('#modalContent').html(data)
});
return false;
});
品牌控制器
public function actionCreate()
{
$model = new Brands();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} elseif (Yii::$app->request->isAjax) {
return $this->renderAjax('create', [
'model' => $model
]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
解决方案
推荐阅读
- graphql - graphql - 参考突变中的其他字段
- flutter - 将数据从另一个类导入到 Stateful Widget
- javascript - 单击主页联系人博客和服务页面时,如何在菜单前显示 div?
- typo3 - TYPO3 - 创建新的 sys_category 时“在页面 [uid] 的根行中找不到伪站点”
- python-3.x - 从容器类中删除元素
- c# - 启动和异步即发即弃呼叫的正确方法?
- ios - How to remove all `^M` characters from an iOS project in Mac copied from Windows?
- elasticsearch - ElasticSearch:给定一个文档和一个查询,相关性得分是多少?
- javascript - 如何在javascript中获取GMT的时区偏移量(如GMT+5:30)
- reactjs - 在 React 中使用本地存储隐藏一些元素