Слетает фильтр после перехода на страницу пагинации

Общие вопросы по использованию второй версии фреймворка. Если не знаете как что-то сделать и это про Yii 2, вам сюда.
Ответить
vanookay
Сообщения: 15
Зарегистрирован: 2020.02.26, 21:27

Слетает фильтр после перехода на страницу пагинации

Сообщение vanookay »

Здравствуйте, имеется простенький фильтр цен, проблема в том, что при переключении на другую страницу фильтр слетает и снова показываются все товары, не знаю как решить
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;
    }
View вывода товаров:

Код: Выделить всё

     
     <?php \yii\widgets\Pjax::begin([
                'id' => 'pjax',
            ]) ?>     
                    <?= $this->render('_filter', ['filter' => $filter]); ?>
                    <?php if (!empty($products)) : ?>
               			Вывод товаров
                                    </div>                                  
                                </div>
                            </div>                        
                        <?=
                        LinkPager::widget([
                            'pagination' => $pages,
                        ]);
                        ?>
View _filter:

Код: Выделить всё

<?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(); ?>
Какие пути решения, кроме навешивания событий и записи в сессию?
yiiliveext
Сообщения: 910
Зарегистрирован: 2019.08.13, 01:49

Re: Слетает фильтр после перехода на страницу пагинации

Сообщение yiiliveext »

Форму надо отправлять методом GET.
Ответить