Bohdan Vorona
Bohdan Vorona

Reputation: 725

Using Pjax widget in Yii2 app. How to update content using links in the layout?

My layout:

Some HTML
All links here won't be working.
...
    <?= $content ?>
...
All links here won't be working.
Some HTML

In each view for each action I'm using Pjax:

<?php
/* @var $this yii\web\View */
use yii\widgets\Pjax;
?>
<?php Pjax::begin(); ?>
All links here will be working.
<?php Pjax::end(); ?>

When I'm clicking on links inside my views - everything is ok, the content is refreshing without reloading of the page.

But how can I use these possibilities when I'm clicking on links outside Pjax widget? For example in my layout?

Upvotes: 2

Views: 1624

Answers (1)

Bohdan Vorona
Bohdan Vorona

Reputation: 725

<ul class="dropdown-menu">
    <li><a href="<?= Url::to('/app/settings/person') ?>" class="js-pjax">Profile details</a></li>
    <li><a href="<?= Url::to('/app/settings/password') ?>" class="js-pjax">Change password</a></li>
</ul>

$(document).on('click', '.js-pjax', function(e){
    e.preventDefault();
    var $this = $(this);
    var href = $this.attr('href');
    var pjax_id = "w0";
    $.pjax.reload({container:'#' + pjax_id, url:href});
    return false;
})

Upvotes: 1

Related Questions