点击按钮弹出包含表单的窗口
1.修改index.php
use yii\bootstrap \Modal;
#code ...
< p>
<?= Html ::button( 'Create Branches', ['value'=>\yii \helpers\ Url::to ('index.php?r=branches/create') , 'class' => 'btn btn-success' ,'id'=> 'modalButton']) ?>
</p>
<?php
Modal ::begin([
'header' =>'<h4>Branches</h4>',
'id' =>'modal',
'size' =>'modal-lg'
]);
echo "<div id='modalContent'></div>" ;
Modal ::end();
?>
2.修改backend/AppAsset.php
class AppAsset extends AssetBundle
{
public $js = [
'js/main.js'
];
}
3.修改backend/web/js/main.js
/**
* Created by Administrator on 2015/5/27.
*/
$(function(){
$ ("#modalButton"). click(function(){
$ ("#modal"). modal('show' )
.find ("#modalContent")
.load ($( this).attr ('value'));
});
});
4.修改控制器代码
/**
* Creates a new Branches model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
if (Yii:: $app->user ->can( 'create-branch')){
$model = new Branches ();
if ($model-> load(Yii ::$app-> request->post ()) ) {
$model ->branch_created_date=date ('Y-m-d H:i:s');
$model ->save();
return $this ->redirect([ 'view', 'id' => $model->branch_id ]);
} else {
/*return $this->render('create', [
'model' => $model,
]);*/
return $this ->renderAjax( 'create', [
'model' => $model,
]);
}
}else {
throw new ForbiddenHttpException ;
}
}