我有一个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(); ?>