I have this code:

<?php /** @var ActiveForm $form */ $form = ActiveForm::begin([ 'id' => 'selectAction' ]); ?> <div class="page__wrapper"> <input type="text"/> <?= $form->field($model, 'action') ->dropDownList([ 'sms' => 'Отправить SMS', 'subscribe' => 'Подписка на контент' ]); ?> <?php Pjax::begin([ 'id' => 'actionSection', ]); switch ($model->action) { case "sms": echo $this->render('form_part_sms'); break; case "subscribe": echo $this->render('form_part_subscribe'); break; default: break; } Pjax::end(); ?> </div> <?php ActiveForm::end() ?> 

I try to make it so that when the attribute of the model changes, the necessary part of the form is loaded via Pjax. After clicking on the link without any problems, it turns out to be done, but I cannot understand how to use change on the dropdown.

Tell me how to tell Pjax, what would it load the desired url by change in select?

    1 answer 1

    Supplemented select with parameters for pjax:

     <?= $form->field($model, 'action') ->dropDownList([ 'sms' => 'Отправить SMS', 'subscribe' => 'Подписка на контент' ], [ 'id' => 'action', 'data-pjax-options' => [ 'sms' => [ 'url' => Url::to(['controller/update-part', 'id' => $model->id, 'part' => 'sms']), ], 'subscribe' => [ 'url' => Url::to(['controller/update-part', 'id' => $model->id, 'part' => 'subscribe']), ], ], ]); ?> 

    And he added the following JS:

     $(function(){ $('#action').change(function(){ let options = $(this) .data('pjax-options')[$(this).find('option:selected').val()]; options.container = '#actionSection'; options.replace = false; // По-умолчанию true, из-за чего в строке браузера изменяется url $.pjax.reload(options); }); }); 

    And here is an action to the controller:

     /** * @param $part * @param null $id * @return string * @throws NotFoundHttpException */ public function actionUpdatePart($part, $id = null) { if (is_null($id)) { $model = new Model(); } else if (!$model = Model::find()->where('id = :id', ['id' => $id])->one()) { throw new NotFoundHttpException("..."); } return $this->renderPartial('controller/form_part_' . $part, [ 'model' => $model, ]); } 

    Thus everything earned, on change in the necessary part of the form is loaded.