Yii2 Pjax重新加载页面

时间:2017-08-14 14:09:10

标签: php yii2 pjax

我有一个Pjax容器,我要在其中加载booksauthors。我的活动表单有两个标签,我可以通过它选择要加载的内容。我在sql中测试了我的phpmyadmin并且它们没问题(返回预期的内容)。在我的控制器的每一行之后尝试了var_dump,但似乎也没问题。但当我谈到“pjax的魔力”时,页面会重新加载。控制台日志中没有错误。 Network选项卡中请求的响应返回整个html。你能看看并给予和推进吗?

控制器操作:

public function actionSingleGenre(){
        $first_step = Yii::$app->getRequest()->getQueryParam('first_step');
        $second_step = Yii::$app->getRequest()->getQueryParam('second_step');
        $id = end(explode('-', $second_step));
        $genresPage = Page::findOne(76);
        $booksPage = Page::findOne(45);
        $authorsPage = Page::findOne(46);
        $lang = Lang::getCurrent();
        $genre = Brands::findOne($id);
        $banner = CategoryImage::find()->where(['page_id' => $genresPage->id])->one();
        $pageSize = 12;
        $entity = 'book';

        $sql = "SELECT *
                 FROM `product` as p 
                 LEFT JOIN `productLang` as pl ON p.`id`=pl.`product_id`
                 LEFT JOIN `book_brand` as bb ON p.`id`=bb.`book_id`
                 WHERE bb.`brand_id`=$id";

        if(Yii::$app->request->isPjax){
            if(isset($_GET['type']) && $_GET['type'] != ''){
                $type = $_GET['type'];
                if($type == 1){
                    $sql = "SELECT *
                                FROM `product` as p 
                                LEFT JOIN `productLang` as pl ON p.`id`=pl.`id`
                                LEFT JOIN `book_brand` as bb ON p.`id`=bb.`book_id`
                                WHERE bb.`brand_id`=$id";
                }
                else if($type == 2)
                {
                    $sql = "SELECT *
                            FROM `author` as a 
                            LEFT JOIN `authorLang` as al ON a.`id`=al.`author_id`
                            WHERE a.`id` IN (
                              SELECT p.`author_id`
                              FROM `product` as p
                              LEFT JOIN `book_brand` as bb ON p.`id`=bb.`book_id`
                              WHERE bb.`brand_id`=$id
                            )";
                    $entity = 'author';
                }
            }
        }

        $count = count(Yii::$app->db->createCommand($sql)->queryAll());

        $dataProvider = new SqlDataProvider([
            'sql' => $sql,
            'totalCount' => $count,
            'pagination' => [
                'pageSize' => $pageSize,
                'route' => $first_step . '/' . $second_step
            ]
        ]);

        $models = $dataProvider->getModels();

        $pagination = new Pagination([
           'pageSize' => $pageSize,
            'totalCount' => $count,
            'route' => $first_step . '/' . $second_step
        ]);

        if(Yii::$app->request->isPjax){
            return $this->renderAjax('single-genre', [
                'genresPage' => $genresPage,
                'authorsPage' => $authorsPage,
                'lang' => $lang,
                'banner' => $banner,
                'booksPage' => $booksPage,
                'genre' => $genre,
                'models' => $models,
                'pagination' => $pagination,
                'entity' => $entity,
            ]);
        }
        return $this->render('single-genre', [
            'genresPage' => $genresPage,
            'authorsPage' => $authorsPage,
            'lang' => $lang,
            'banner' => $banner,
            'booksPage' => $booksPage,
            'genre' => $genre,
            'models' => $models,
            'pagination' => $pagination,
            'entity' => $entity,
        ]);
    }

jQuery的:

function pjaxFilterForm() {
    var dataString = $("#filter-group2").serialize();
    $.pjax.defaults.timeout = false;
    $.pjax({
        container: "#booksGrid",
        url: location.href.split("?")[0],
        data: dataString,
        scrollTo: false
    });
    return false;
}

查看:

   <div class="filter-tags-holder">
    <?php \yii\bootstrap\ActiveForm::begin([
            'method' => 'get',
            'action' => '#',
            'options' => ['data-pjax'=>true, 'onsubmit'=>'return pjaxFilterForm()', 'id'=>'filter-group2']
    ]) ?>
    <div id="submit-helper-tab"></div>
    <input type="hidden" name="type">
    <ul id="filterGenresList" class="option-set">
        <li><a class="selected" data-value="1" href="#"><?= Yii::t('app', 'app.Books') ?></a></li>
        <li><a data-value="2" href="#"><?= Yii::t('app', 'app.Authors') ?></a></li>
    </ul>
    <?php \yii\bootstrap\ActiveForm::end(); ?>
</div>
<!-- Filter Nav -->

<!-- Recommended -->
<?php \yii\widgets\Pjax::begin(['id'=>'booksGrid']) ?>
    <?php if($models) : ?>
    <div id="filter-masonry" class="gallery-masonry">
        <?php foreach ($models as $model){
            if($entity == 'book'){
                $model = \backend\modules\products\models\Product::findOne($model['product_id']);
                echo $this->render('_authorBooks', ['book' => $model, 'lang' => $lang, 'booksPage' => $booksPage]);
            }else if($entity == 'author'){
                $model = \backend\models\Author::findOne($model['author_id']);
                echo $this->render('_authorGenre', ['book' => $model, 'lang' => $lang, 'authorsPage' => $authorsPage]);
            }
        } ?>
    </div>
    <div class='pagination-holder'>
        <?= \yii\widgets\LinkPager::widget([
            'pagination' => $pagination,
            'hideOnSinglePage' => true,
            'prevPageLabel' => Yii::t('app', 'app.Prev'),
            'nextPageLabel' => Yii::t('app', 'app.Next')
        ]) ?>
    </div>
    <?php endif; ?>
<?php \yii\widgets\Pjax::end() ?>

0 个答案:

没有答案