dsgdfg30210 2016-09-17 19:16
浏览 149

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

I've tried for created form update using modal in yii2. The problem is every I change one field with the other value, the result form update with modal closed and redirect to index. And message update can't display in index. I don't know why, maybe wrong in code controller.

Code actionUpdate in controller

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]);
                }
            }
    }

Code form in view

<?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();?>

Code index.php in view

<?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,  
        });  
    })
');    
?>

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛