Yii2 Pjax无法正常工作

时间:2017-08-11 11:15:32

标签: php yii2 pjax

我有一个pjax表单,我应该用他们名字的第一个字母过滤书籍作者。但每次pjax刷新页面。控制台中没有错误,无法在network选项卡中看到错误请求(可能因为刷新而无法看到它)。经过几次测试后,我了解到问题出现在代码$authorModels = $dataProvider->getModels();的数据行中。如果我var_dump之前没有问题,那么在此之后没有var_dump响应。 ($authorModels = $dataProvider->getModels();var_dump($authorModels);die;,页面会刷新。这是我的行动:

public function actionAuthors(){

$authorsPage = Page::findOne(46);
$banner = CategoryImage::find()->where(['page_id' => 46])->one();
$lang = Lang::getCurrent();
$randomAuthors = Author::find()->where('active=1')->orderBy(new Expression('rand()'))->limit(5)->all();
$randomBooks = Product::find()->where('active=1')->orderBy(new Expression('rand()'))->limit(4)->all();
$bookPage = Page::findOne(45);
$en_arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
$bg_arr = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', ' й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ш', 'щ', 'ъ', 'ю', 'я'];
$array_in_use = [];
$pageSize = 5;

if(Yii::$app->request->isPjax){

    if(isset($_GET['letter']) && $_GET['letter'] != ''){

        $letter = '';
        if($lang->url == "en"){
            $letter = $en_arr[$_GET['letter']];
        }else if($lang->url == "bg"){
            $letter = $bg_arr[$_GET['letter']];
        }

        $sql = Yii::$app->db->createCommand("SELECT * 
                                             FROM `author` as a
                                             LEFT JOIN `authorLang` as al ON a.`id`=al.`author_id`
                                             WHERE a.`active`=1 
                                             AND al.`language`='$lang->url'
                                             AND al.`names` LIKE '$letter%'")
                                            ->queryAll();
        $countAuthors = count($sql);

        $dataProvider = new SqlDataProvider([
            'sql' => $sql,
            'totalCount' => $countAuthors,
            'pagination' => [
                'pageSize' => $pageSize,
                'route' => Yii::$app->getRequest()->getQueryParam('first_step')
            ]
        ]);

        $authorModels = $dataProvider->getModels();

        $pagination = new Pagination([
           'totalCount' => $countAuthors,
            'pageSize' => $pageSize,
            'route' => Yii::$app->getRequest()->getQueryParam('first_step')
        ]);

        return $this->renderPartial('authors', [
            'pagination' => $pagination,
            'authorModels' => $authorModels,
        ]);
    }
}

我的js:

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

视图:

<?php
                        ActiveForm::begin([
                            'action' => '#',
                            'method' => 'get',
                            'options' => ['data-pjax' => true, 'onsubmit' => 'return pjaxFilterLetters();', 'id' => 'charsForm',],
                        ]);
                        ?>
                        <input type="hidden" name="letter" />
                        <div class="authors-filter">
                            <ul class="letters">
                            <?php
                                $key = 0;
                                foreach ($array_in_use as $char){
                                    echo "<li value='{$key}'>{$char}</li>";
                                    $key++;
                                }
                            ?>
                            </ul>
                        </div>
                        <?php ActiveForm::end(); ?>
                        <!-- Author Filter -->

                        <!-- Author List -->
                        <?php Pjax::begin(['timeout' => false, 'id' => 'productGrid', 'scrollTo' => false]);?>
                        <ul class="author-list">
                            <li>
                                <?php
                                    if(isset($authorModels) && !empty($authorModels)){
                                        foreach ($authorModels as $author) {
                                            //here are coming the authors
                                        }
                                    }
                                    else
                                    {
                                        echo Yii::t('app', 'app.No authors found');
                                    }
                                ?>
                            </li>
                        </ul>
                        <?php Pjax::end(); ?>

0 个答案:

没有答案