B
B
Bogdan Pasechnik2016-05-16 17:08:06
Yii
Bogdan Pasechnik, 2016-05-16 17:08:06

Yii2. How to register css when using pjax?

The question concerns only when working on the yii2 framework.
I'll be as brief as possible with the code. This is the view code.

\yii\widgets\Pjax::begin(['id' => 'pjax-container']);
if(Yii::$app->request->isPjax) {
    echo \yii\jui\DatePicker::widget([
        'name' => 'date',
        'value' => '',
    ]);
} else {
    echo \yii\helpers\Html::button('Load Pjax', ['onclick' => "$.pjax.reload({container:'#pjax-container'});"]);
}
\yii\widgets\Pjax::end();

The bottom line is that when the css page is loaded, jquery-ui will not load because the Yii::$app->request->isPjax condition does not pass.
Therefore, there will be no corresponding styles in the page header. Further when I do reload through pjax the condition passes. And the styles should load. But only js files and js code are connected.
And if I do it without using pjax and call renderAjax in the controller. In this case, everything works fine.
An even simpler example
\yii\widgets\Pjax::begin(['id' => 'pjax-container']);
if(Yii::$app->request->isPjax) {
    $this->registerCssFile('http://test.css'); // не регистрируется...
    $this->registerJsFile('http://test.js'); // регистрируется
}
echo \yii\helpers\Html::button('Load Pjax', ['onclick' => "$.pjax.reload({container:'#pjax-container'});"]);
\yii\widgets\Pjax::end();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-05-16
@webinar

1. Yii::$app->request->isPjax- it should be in the controller, not in the view.
2. PJAX was not invented to hide blocks. Why is pjax. Do a normal render and use js to show a block with datepiker-om or bootstrap collapse getbootstrap.com/javascript/#collapse
3.

And if I do it without using pjax and call renderAjax in the controller. In this case, everything works fine.
- because this is done without using PJAX, do you even understand why it is and how it differs from AJAX?
4. Everything works for you exactly as it should. It's just that you want to scratch your nose through your ass, and the authors of the framework did not think that such an idea would come to someone's mind.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question