I
I
Ivan Lykov2018-04-07 12:55:18
Yii
Ivan Lykov, 2018-04-07 12:55:18

How to display a piece of content using Pjax yii2?

Good afternoon, question.
There is a site bar. It has categories and a search form. Search form - select2 is already screwed and working when entering into the form, it produces results from the database.
The question is how to implement such a thing, I can not understand. When entering data into the search string, in the right column, the result was displayed in the left column.
screen for understanding
5ac8955137320970910120.png
I looked at pjax examples, here https://nix-tips.ru/yii2-vnikaem-v-pjax.html
But I didn’t figure out where and how to wrap the widget for it to work
, in fact, send data from one form , get dynamic into another...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-04-07
@Jhon_Light

Good afternoon.
Do you have a drop down list?

<select name="spjax" id="spjax">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

Somewhere else on the page there is a block that needs to be updated. When updating, the value of the $Ipjax variable must be added to the header. We wrap this Pjax block and add custom js.
<?php
   Pjax::begin([
       'id' => 'view-mode-pjax'
   ])
?>
<div class="col-lg-4">
    <h2>Heading <?= $Ipjax ?></h2>

    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
        dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
        ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
        fugiat nulla pariatur.</p>

    <p><a class="btn btn-default" href="http://www.yiiframework.com/doc/">Yii Documentation &raquo;</a></p>
</div>
<?php
  Pjax::end();
$this->registerJs("
$(function(){
  $('#spjax').on('change', function(){
    var Id = $(this).val()
    console.log(Id)
    $.pjax.reload({
        container: \"#view-mode-pjax\", // контейнер, в котором надо обновить данные.
        url: window.location.href,
        timeout: 0,
        data: {
           'id': Id // данные, которые отправляются на сервер.
        },
    });
  })  
})    
", View::POS_END)
?>

And in the controller, in the action that connects this view, you pass the desired variable.
public function actionIndex()
{
    $Ipjax = Yii::$app->request->get('id') ?: null;
    return $this->render('index', ['Ipjax' => $Ipjax]);
}

You catch the change in the dropdown list and run pjax. There is a request to the server, the variable is checked and the same view is returned, but with the variable.
And Pjax, a cunning animal, will not update the entire page, but only the block that you specify.
ps The example is made in yii2-basic, in the views/index file and in the DefaultController.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question