S
S
Silverviql2018-09-18 12:55:24
Yii
Silverviql, 2018-09-18 12:55:24

How to call modal window from controllera in view?

Good afternoon everyone! It is necessary to call the modal window from the controller in the view.
In the controller the following function

public function actionFulfilled($id)
    {
        $model = $this->findModel($id);
        $model->status = Zakaz::STATUS_EXECUTE;
        $model->id_unread = 0;
        if($model->fact_oplata === null ||  $model->fact_oplata === 0){
            Yii::$app->session->addFlash('errors', 'Нельзя закрыть заказ с оплатой 0!');
            $modalErrorOplata = true;
            return $this->redirect(['admin']);

        } else {
            if ($model->save()) {
                Yii::$app->session->addFlash('update', 'Выполнен заказ №'.$model->prefics);
                return $this->redirect(['admin']);
            }else {
                print_r($model->getErrors());
                Yii::$app->session->addFlash('errors', 'Произошла ошибка!');
            }
        }
    }

in a view
<?php
var_dump($modalErrorOplata);
if($modalErrorOplata === true){
$script = <<<JS
        function modalView(button, modal) {
                $('body').on('click', button, function(e){
                    e.preventDefault();
                    $(modal).modal('show')
                        .find('.modalContent')
                        .load($(this).attr('value'));
                });
            }
            
            modalView('.modalZakazview-button', '#modalZakazview');
JS;
$this->registerJs($script);
}
?>

<?php Modal::begin([
    'id' => 'modalZakazview',
    'header' => '<h2>Полный просмотр</h2>'
]);
echo '<div class="modalContent"></div>';
Modal::end(); ?>

I understand that $modalErrorOplata = true; in the controller and checking it in the view is complete garbage and so the parameter is not passed. It is necessary to somehow put it in a redirect or declare it in the controller and somehow read it in the view....

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-09-18
@slo_nik

Good afternoon.
You can pass the parameter like this
But! Modal window you open after clicking. When redirecting, you need to somehow simulate a click on the button. I think it would be better to look towards ajax sending data and receiving a response.

M
Maxim Timofeev, 2018-09-18
@webinar

The controller generates html based on the view and sends it to the browser in response to a request from the browser. The modal exists when js starts to work, that is, in the browser. So your question is completely wrong.
Given that you have, .load($(this).attr('value'));
as I understand it, ajax is going somewhere, so apparently, according to the result of this ajax, you either need to open or not a modal. If you formulate the problem completely, and not your vision of its solution, we may find a more correct algorithm.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question