如何更新页面的另一部分(使用PJAX)?

时间:2016-02-26 09:43:00

标签: php yii2 pjax

我在layouts / main.php文件中有这段代码:

<div class="wrap">
<? \yii\widgets\Pjax::begin(['id' => 'nav_pjax'])?>
<?php
NavBar::begin([
    'brandLabel' => 'Название компании',
    'brandUrl' => Yii::$app->homeUrl,
    'options' => [
        'id' => 'ww',
        'class' => 'navbar-inverse navbar-relative',
    ],
]);

echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right', 'id' => 'ww'],
    'items' => User::getMenuItemsByRoleUser(!Yii::$app->user->isGuest ? Yii::$app->user : false,Yii::$app->user->isGuest)
]);
NavBar::end();
?>
<? \yii\widgets\Pjax::end()?>
<div class="container" id="content_inner">
    <? \yii\widgets\Pjax::begin(['timeout' => '3000'])?>
    <?= Breadcrumbs::widget([
        'homeLink'=>[
            'label' => 'Главная',  // required
            'url' =>  \yii\helpers\Url::home(),      // optional, will be processed by Url::to()
        ],
        'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
    ]) ?>

    <?= $content ?>
    <? \yii\widgets\Pjax::end()?>
</div>

当我点击导航栏链接时,服务器返回一个内容,我想将其放入div class =&#34; container&#34; ID =&#34; content_inner&#34 ;.我怎么能这样做?或者我应该完全刷新页面?

1 个答案:

答案 0 :(得分:2)

您不需要两个pjax小部件,您可以尝试:

1)仅更新内容

<div class="wrap">

        <?php /* navbar */ ?>

        <div class="container" id="content_inner">
            <?php \yii\widgets\Pjax::begin([
                'id' => 'nav_pjax',
                'timeout' => '3000',
                'linkSelector' => '.navbar-nav a',  // use pjax on navbar links
            ]); ?>

                <?php /* breadcrumb and content */ ?>

            <?php \yii\widgets\Pjax::end(); ?>
        </div>

</div>

详细了解linkSelector

2)更新导航栏和内容

<div class="wrap">
    <?php \yii\widgets\Pjax::begin([
        'id' => 'nav_pjax',
        'timeout' => '3000',
    ]); ?>

            <?php /* navbar */ ?>

            <div class="container" id="content_inner">
                <?php /* breadcrumb and content */ ?>
            </div>

    <?php \yii\widgets\Pjax::end(); ?>
</div>