ActiveForm和GridView上的Yii2 Pjax [搞定了]

时间:2016-01-07 09:04:04

标签: yii2 pjax

index page
Modal when clicked on remove member
when form submitted
我有一个gridview

<?php Pjax::begin(['id' => 'members']) ?> 

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        // 'filterModel' => $searchModel,
        'columns' => [
        ['header'=>'Remove Member',
                        'value'=> function($data)
                        { 
                            return  Html::a(Yii::t('app', ' {modelClass}', [
                                'modelClass' =>'',
                            ]), ['members/stl_remove','id'=>$data->id], 
                            ['class' => 'btn btn-link fa fa-times fa-2x pop']
                            );  
                        },
            'format' => 'raw'
            ],
      ['class' => 'yii\grid\ActionColumn','template'=>'{update}'],
        ],
    ]); ?>
    <?php Pjax::end() ?>

当我点击删除成员链接模式弹出窗口(delete.php)

<?php Pjax::begin(['id' => 'delete_members']) ?>

    <?php $form = ActiveForm::begin(['options' => ['data-pjax' => true ]]); ?> 

    <div class="row">
        <div class="col-md-5">

            <?php echo $form->field($model, 'remarks')->dropDownList(['death' => 'Death',
                'marriage' => 'Marriage', 'house_shift' => 'House Shift'],['prompt'=>'Select'])
                ->label("Reason For Removal"); ?>

        </div>
    </div>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Remove') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

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

现在这些是我的控制器代码 [代码]

public function actionStl_remove($id,$ajax=0)
    {
        $model              =   $this->findModel($id);
        $model_delete       =   new Members();
        $model->scenario    =   "remove";

        if ($model_delete->load(Yii::$app->request->post())) 
        {
            $model->remarks =   $model_delete->remarks;
            $model->status  =   1;
            $model->save();
            // $model = new Members();
            // return $this->redirect(['index']);
        }
        else
        {
        return $this->renderAjax('delete', [
                'model' =>  $model_delete,
                'id'    =>  $id,
            ]);
        }
    }

我无法将数据加载到gridview中。我点击链接模式弹出窗口然后我提交后我继续索引页面和页面重新加载

1 个答案:

答案 0 :(得分:4)

这就是我如何运作的方式。在 delete.php 中,我做了以下更改。

<?php $form = ActiveForm::begin([
    'options' => ['data-pjax' => true,
                  'id'=> 'dynamic-form111',
                  // 'validationUrl' => 'validation-rul'
                   ]]); ?> 

    <div class="row">
        <div class="col-md-5">

            <?php echo $form->field($model, 'remarks')->dropDownList(['death' => 'Death',
                'marriage' => 'Marriage', 'house_shift' => 'House Shift'],['prompt'=>'Select'])
                ->label("Reason For Removal"); ?>

        </div>
    </div>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Remove') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

基本上我给了来自dynamic-form111的id。 然后在 index.php 和pjax中的gridview中添加以下代码

<?php
$this->registerJs(
    'jQuery(document).ready(function($){
                $(document).ready(function () {
                    $("body").on("beforeSubmit", "form#dynamic-form111", function () {
                        var form = $(this);
                        // return false if form still have some validation errors
                        if (form.find(".has-error").length) 
                        {
                            return false;
                        }
                        // submit form
                        $.ajax({
                            url    : form.attr("action"),
                            type   : "post",
                            data   : form.serialize(),
                            success: function (response) 
                            {

                                $("#ajaxModal").modal("toggle");
                                $.pjax.reload({container:"#countries"}); //for pjax update

                            },
                            error  : function () 
                            {
                                console.log("internal server error");
                            }
                        });
                        return false;
                     });
                    });

    });'
);
?> 

通过ajax检查delete.php是否已成功提交,如果是,则使用pjax重新加载gridview并关闭模态。