Yii2 Pjax即使使用renderPartial也会重新加载页面

时间:2018-10-21 18:03:57

标签: yii2 yii2-advanced-app pjax

我正在发送Pjax请求,即使我正在使用renderPartial,该请求也会重新加载整个页面。 pjax container#promo-products,为什么它会重新加载整个页面,而不只是用所需的结果填充容器?如果我var_dump($child_products);die;在容器中显示结果,但没有die,它将重新加载页面。还尝试渲染局部视图(例如说_children),但结果是相同的。我怎么了 控制器:

public function actionCreate()
    {
        $model = new Promotion();
        $products = [];
        $success = null;
        $child_products = [];

        if(Yii::$app->request->isPjax){
            $parent_id = Yii::$app->request->get('parent_id');
            if($parent_id){
                $parent_children = ProductChild::find()->where(['id_in' => $parent_id])->all();
                foreach ($parent_children as $child){
                    $child_products[$child['id']][] = $child;
                }
            }
        }
        if (!Yii::$app->request->isPjax && $model->load(Yii::$app->request->post())) {

            $transaction = Yii::$app->db->beginTransaction();

            try{
                if($model->save()){
                    //save products
                    $model->saveProducts();

                    $transaction->commit();
                    return $this->redirect(['index', 'success' => 1]);
                } else {
                    throw new \Exception("Some error occurred.");
                }
            }catch (\Exception $e){
                $success = 3;
                $model = new Promotion();
                $transaction->rollBack();
            }

        }

        if(Yii::$app->request->isPjax){
            return $this->renderPartial('create', [
                'child_products' => $child_products,
            ]);
        }
        return $this->render('create', [
            'model' => $model,
            'products' => $products,
            'success' => $success,
            'child_products' => $child_products,
        ]);
    }

我的观点:

<?php

use yii\widgets\ActiveForm;
use backend\helpers\Header;
use kartik\select2\Select2;
use yii\web\JsExpression;
use backend\helpers\CGrowl;

\backend\assets\JquerySteps::register($this);
\backend\assets\DateTimePickerAsset::register($this);

/* @var $this yii\web\View */
/* @var $model backend\models\Contact */
/* @var $form yii\widgets\ActiveForm */

$this->registerJs("
    $('#promotion-start_date').bootstrapMaterialDatePicker({ format: 'YYYY-MM-DD HH:mm:ss' });
    $('#promotion-end_date').bootstrapMaterialDatePicker({ format: 'YYYY-MM-DD HH:mm:ss' });
    ",
    \yii\web\View::POS_LOAD,
    'datetime-picker-init'
);

if (isset($success)) {
    echo CGrowl::setFormResult((int)$success, $this);
}

?>

<div class="row page-titles" style="background-image: url('<?= Yii::getAlias('@backend-image') ?>/theme/images/heading-title-bg.jpg')">
    <div class="col-md-12">
        <h4 class="text-white"><?= Yii::t('app', 'Promotion') ?></h4>
    </div>
    <div class="col-md-6">
        <?= Header::renderBreadcrumbs($this) ?>
    </div>
</div>

<div class="row" id="validation">
    <div class="col-12">
        <div class="card wizard-content">
            <div class="card-body">

                <div class="panel-body">

                    <?php $form = ActiveForm::begin([
                        'options' => [
                            'class' => 'validation-wizard wizard-circle'
                        ],
                    ]); ?>

                    <h6><?= Yii::t('app', 'Main info') ?></h6>

                    <section>

                        <div class="row">
                            <div class="col-sm-6">
                                <?= $form->field($model, 'type')->widget(Select2::class, [
                                    'data' => [
                                        1 => Yii::t('app', 'Percentage'),
                                        2 => Yii::t('app', 'Price'),
                                    ],
                                    'options' => [
                                        'placeholder' => Yii::t('app', 'Type')
                                    ],
                                    'pluginOptions' => [
                                        'allowClear' => true
                                    ]
                                ]) ?>
                            </div>
                            <div class="col-sm-6">
                                <?= $form->field($model, 'value')->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-6">
                                <?= $form->field($model, 'start_date')->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-6">
                                <?= $form->field($model, 'end_date')->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-4">
                                <?= $form->field($model, 'products')->widget(Select2::class, [
                                    'initValueText' => $products,
                                    'options' => [
                                        'multiple' => true
                                    ],
                                    'pluginOptions' => [
                                        'allowClear' => true,
                                        'minimumInputLength' => 3,
                                        'language' => [
                                            'errorLoading' => new JsExpression("function(){return 'Waiting for results ...'; }"),
                                        ],
                                        'ajax' => [
                                            'url' => \yii\helpers\Url::to(['async/products']),
                                            'dataType' => 'json',
                                            'data' => new JsExpression("function(params){return {q:params.term}; }")
                                        ],
                                        'escapeMarkup' => new JsExpression("function(markup){return markup;}"),
                                        'templateResult' => new JsExpression("function(city){return city.text;}"),
                                        'templateSelection' => new JsExpression("function(city){return city.text;}")
                                    ],
                                    'pluginEvents' => [
                                        'select2:select' => "function(){
                                            let url = location.href.split('?')[0]
                                            $.pjax.defaults.timeout = false
                                            $.pjax({
                                                url: url,
                                                data: {
                                                    parent_id: this.value
                                                },
                                                container: '#promo-products'
                                            })
                                        }"
                                    ]
                                ]) ?>
                            </div>
                            <div class="col-sm-8">
                                <label for=""><?= Yii::t('app', 'Varieties')?></label>
                                <div class="table-responsive">
                                    <table>
                                        <?php \yii\widgets\Pjax::begin(['id' => 'promo-products']) ?>
                                        <tbody>
                                        <?php
                                            foreach ($child_products as $child){
                                                echo $child->id;
                                            }
                                        ?>
                                        </tbody>
                                        <?php \yii\widgets\Pjax::end() ?>
                                    </table>
                                </div>
                            </div>
                            <div class="col-sm-12">

                            </div>
                        </div>

                    </section>

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

                </div>
            </div>
        </div>
    </div>
</div>

0 个答案:

没有答案