Action:
Код: Выделить всё
$category = Category::find()->where(['slug' => $slug])->one();
$filter = new Filter();
$filter->load(Yii::$app->request->post());
$query = Product::findCategoryWithFilter($filter, $category);
$pages = new Pagination([
'totalCount' => $query->count(),
'pageSize' => 6,
'forcePageParam' => false,
'pageSizeParam' => false,
]);
$products = $query->offset($pages->offset)->limit($pages->limit)->all();
if (Yii::$app->request->isAjax) {
return $this->renderAjax('view', compact('products', 'category', 'pages', 'filter'));
}
return $this->render('view', compact('products', 'category', 'pages', 'filter'));
Код: Выделить всё
public static function findCategoryWithFilter($filter, $category)
{
$query = Product::find()
->where(['and',
['>', 'price', $filter->price_from],
['<', 'price', $filter->price_to ? $filter->price_to : Product::find()->max('price')]])
->andWhere(['category_id' => $category->id])
return $query;
}
Код: Выделить всё
<?php \yii\widgets\Pjax::begin([
'id' => 'pjax',
]) ?>
<?= $this->render('_filter', ['filter' => $filter]); ?>
<?php if (!empty($products)) : ?>
Вывод товаров
</div>
</div>
</div>
<?=
LinkPager::widget([
'pagination' => $pages,
]);
?>
Код: Выделить всё
<?php
$form = \yii\widgets\ActiveForm::begin([
'id' => 'test-form',
'options' => [
'data-pjax' => true,
]
]);
?>
<?= $form->field($filter, 'price_from') ?>
<?= $form->field($filter, 'price_to') ?>
<?= \yii\helpers\Html::submitButton('Вывести', ['class' => 'btn btn-success']) ?>
<?php \yii\widgets\ActiveForm::end(); ?>