1、gridview的操作增加[更新]按钮,并指定data-toggle data-target class以及data-id的值?
[ 'class' => 'yii\grid\ActionColumn', 'template' => '{update}', 'buttons' => [ 'update' => function ($url, $model, $key) { return Html::a('更新', '#', [ 'data-toggle' => 'modal', 'data-target' => '#update-modal', 'class' => 'data-update', 'data-id' => $key, //所传参数 ]); }, ], ],
2、为更新添加modal?
<?php use yii\bootstrap\Modal; // 更新操作 Modal::begin([ 'id' => 'update-modal', 'header' => '<h4 class="modal-title">更新</h4>', 'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>', ]); $requestUpdateUrl = Url::toRoute('update'); $updateJs = <<<JS $('.data-update').on('click', function () { $.get('{$requestUpdateUrl}', { id: $(this).closest('tr').data('key') }, function (data) { $('.modal-body').html(data); } ); }); JS; $this->registerJs($updateJs); Modal::end(); ?>
3、修改我们的update方法
public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->renderAjax('update', [ 'model' => $model, ]); } }