使用yii2中的模态进行表单更新

时间:2016-09-17 19:16:12

标签: php forms yii modal-dialog yii2

我已尝试使用yii2中的modal创建表单更新。问题是每次我用另一个值改变一个字段,结果形式更新,模态关闭并重定向到索引。并且消息更新无法在索引中显示。我不知道为什么,也许在代码控制器中出错了。

控制器中的代码actionUpdate

public function actionUpdate($id)
	{
		$model = CalonKeluargaAsuh::findOne($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
                Yii::$app->session->setFlash('success', 'Data berhasil diubah!');
                return $this->redirect(['index']);
                return $this->refresh();
            } else {
                if (Yii::$app->request->isAjax) {
                    return $this->renderAjax('update', ['model' => $model]);
                }
                else{
                    return $this->render('update', ['model' => $model]);
                }
            }
	}

视图中的代码表格

<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\widgets\Pjax;

?>

<h2 align="center">Ubah Data Calon Keluarga Asuh</h2>
<?php
echo "&nbsp";
echo "&nbsp";
?>
<?php $form = ActiveForm::begin(['layout' => 'horizontal', 'enableAjaxValidation' => true,
    'id' => 'update-form',
    ]); ?>
<?= $form->field($model, 'kode_calon_keluarga_asuh')->textInput(['readOnly' => true, 'style' => 'width:350px' ]) ?>
<?= $form->field($model, 'nama')->textInput(['style' => 'width:350px']) ?>
<?= $form->field($model, 'jenis_kelamin')->dropDownList(['Laki-laki' => 'Laki-laki', 'Perempuan' => 'Perempuan'],
    ['prompt'=>'--Pilih--', 'style' => 'width:350px']) ?>
<?= $form->field($model, 'pekerjaan')->textInput(['style' => 'width:350px']) ?>
<?= $form->field($model, 'alamat')->textArea(['rows' => 3, 'style' => 'width:350px']) ?>
<?= $form->field($model, 'telepon')->textInput(['style' => 'width:350px']) ?>   
<div class="form-group">
    <div class="col-sm-offset-4">
<?= Html::submitButton('Ubah', ['class' => 'btn btn-primary']) ?>
<?php
echo "&nbsp";
echo "&nbsp"; 
echo Html::a('Keluar', ['index'],[
	'class'=>'btn btn-success',
	'onclick' =>'$("#calonModal").modal("hide");
	return false;'
	]);
?>
<?php ActiveForm::end();?>

视图中的代码index.php

<?php \yii\widgets\Pjax::begin(['timeout' => false, 'id' => 'pjax-gridview']); ?>

<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\bootstrap\Modal;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel app\models\SearchDonatur */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Data Calon Keluarga Asuh';
?>

<?php if (Yii::$app->session->hasFlash('success')): ?>
  <div class="alert alert-success alert-dismissable">
  <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
  <h4><i class="icon fa fa-check"></i>Informasi!</h4>
  <?= Yii::$app->session->getFlash('success') ?>
</div>
<?php endif; ?>


<?php if (Yii::$app->session->hasFlash('delete')): ?>
  <div class="alert alert-success alert-dismissable">
  <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
  <h4><i class="icon fa fa-check"></i>Informasi!</h4>
  <?= Yii::$app->session->getFlash('delete') ?>
  </div>
<?php endif; ?>


<div class="calon-keluarga-asuh-index">

    <?php Pjax::begin(['timeout'=>false,'id'=>'pjax-gridview']); ?>

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
       <?= Html::a('Tambah Data', ['create'], ['class' => 'btn btn-success']) ?>
    </p>  

    <?= GridView::widget([
    	'dataProvider' => $dataProvider,
    	'filterModel' => $searchModel,
    	'emptyCell' => '-',
    	'summary' => '',
    	'columns' => [
    	    [
    	        'attribute' => 'kode_calon_keluarga_asuh',
    	        'value' => 'kode_calon_keluarga_asuh',
    	        'contentOptions' => ['style' => 'width: 100px;']
    	    ],

    	    [
                'attribute' => 'nama',
    	        'value' => 'nama',
    	        'contentOptions' => ['style' => 'width: 250px;']
    	    ],

    	    [
                'attribute' => 'jenis_kelamin',
    	        'value' => 'jenis_kelamin',
    	        'contentOptions' => ['style' => 'width: 100px;']
    	    ],

    	    [
                'attribute' => 'pekerjaan',
    	        'value' => 'pekerjaan',
    	        'contentOptions' => ['style' => 'width: 200px;']
    	    ],

    	    [
                'attribute' => 'alamat',
    	        'value' => 'alamat',
    	        'contentOptions' => ['style' => 'width: 210px;']
    	    ],

    	    [
                'attribute' => 'telepon',
    	        'value' => 'telepon',
    	        'contentOptions' => ['style' => 'width: 100px;']
    	    ],
            

            [
                'class' => \yii\grid\ActionColumn::className(),
                'header' => 'Aksi',
                'template' => '{update} {delete}',
                'buttons' => [
                    'update' => function($url, $model) {
                    $icon = '<span class="glyphicon glyphicon-pencil"></span>';
                    return Html::a($icon, $url,[
                        'data-toggle' => "modal",
                        'data-target' => "#calonModal",
                        ]);  
                },

                'delete' => function($url, $model) {
                    $icon = '<span class="glyphicon glyphicon-trash"></span>';
                    return Html::a($icon, $url, 
                    [
                        'data-confirm' => "Apakah yakin dihapus ?",
                        'data-method' => 'post',    
                    ]);
                },
                ]
            ],
    	],
    ]); ?>

    <?php \yii\widgets\Pjax::end() ?>
    <?php Pjax::end(); ?>

</div>
<?php
Modal::begin(['id' => 'calonModal']);
    Pjax::begin(['id'=>'pjax-modal', 'timeout'=>false,
        'enablePushState'=>false,
        'enableReplaceState'=>false,]);

    Pjax::end();
Modal::end();  
?>

<?php
$this->registerJs('
    $("#calonModal").on("shown.bs.modal", function (event) {
        var button = $(event.relatedTarget)
        var href = button.attr("href")
        $.pjax.reload("#pjax-modal", {
            "timeout":false,
            "url":href,
            "replace":false,  
        });  
    })
');    
?>

0 个答案:

没有答案