第一次使用,时候不明白什么原理,大概用了几次后,才模模糊糊搞清楚原来是怎么一回事,现在就把写过的代码,贴在下边。
1.在视图文件中,
第一步首先在index.php文件中
做了一个a链接的按钮 调用了modal <a class="btn btn-default pull-right" data-remote="<?= Url::toRoute(['create', '_wicket' => 'yes']) ?>" data-toggle="modal" data-target="#myModal" role="button">添加地区</a> <!--这里是其他代码--> <!-- Modal部分 --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="progress" style="margin:0;"><div class="progress-bar progress-bar-info progress-bar-striped active" role="progressbar"
aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style=" 1%"></div></div> </div> </div> </div>
第二步其次,在create.php文件中
<?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/10/20 * Time: 10:54 */ use yiihelpersHtml; use yiiwidgetsActiveForm; Main::register($this); ?> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="myModalLabel">添加地区</h4> </div> <?php $form = ActiveForm::begin([ 'options' => [ 'class' => '', 'enctype'=>'multipart/form-data', 'method'=>'post', 'fieldConfig'=>[ 'template' => "{label} <div class="col-lg-3">{input}</div> <div class="col-lg-8">{error}</div>", ] ] ]); ?> <div class="modal-body"> <!--其他代码-->如表单输入框等 </div> </div> </div> <div class="modal-footer" style="border-top:0px"> <button type="button" class="btn btn-primary submit-ajax col-lg-offset-2" data-loading-text="加载中..." autocomplete="off">确认</button> <button type="button" class="btn btn-default" data-dismiss="modal">取消</button> </div> <?php ActiveForm::end(); ?>
2.在控制器中
在create.php中
public function actionCreate(){ $model = City::findOne($id);
//其他代码 return $this->render('create',[ 'model'=>$model ]); }
注意:有时候要用到ajax传递modal中的数据,ajax会把值传递到index中去,所以需要在index控制器中去进行相应的处理。
转载请注明出处。