yii 2 pagination is losing filter data in the model

548 views Asked by At

Right, I have a list view that shows content from my database. The pagination works great with this. However as soon as i apply filters to my data provider and try to paginate to the second page for example, it forgets all filters and goes to the 2nd page of all the content.

has anyone experienced this?

If you need code please do ask and i will provide but it would be a hell of a lot to go through

View:

        <table class="table table-striped">
            <tr class="top-tr">
                <th width="50%">Case name</th>
                <th>Primary citation</th>
                <th>Bailii.org</th>
                <th>Adj.co.uk</th>
                <?php if(isset($chapters)) { ?>
                <th>Para(s)</th>
                <?php } ?>
            </tr>
            <?php
            if(!isset($chapters)) {

                echo listView::widget([
                    'dataProvider' => $dataProvider,
                    'itemView' => function ($model, $chapters) {
                        return '<tr><td><a href="/web/cases/view?id='. $model->case_id .'">' . $model->name . '</td>' .
                        '<td>' . $model->getCitation() . '</td>' .    
                        (strtolower($model->bailli) != "not available" ? '<td><a class="external-link" href=' . $model->bailli . ' target="_blank">Visit site</a></td>' : '<td></td>') .
                        (strtolower($model->adjudication_URL) != "not available" ? '<td><a class="external-link" href=' . $model->adjudication_URL . ' target="_blank">Visit site</a></td></tr>' : '<td></td></tr>');
                    },
                ]);

            } else {
                echo "<pre>";
                print_r($chapters);
                echo "</pre>";
                if(isset($category) && $category !== null) {
                    echo '<h3>Sub-section: ' . $category[0]['name'] . '</h3>';

                }elseif(isset($subcategory) && $subcategory !== null){
                    echo '<h3>Section: ' . $subcategory[0]['name'] . '</h3>';
                }elseif(isset($firstcategory) && $firstcategory !== null){
                    echo '<h3>Chapter: ' . $firstcategory[0]['name'] . '</h3>';
                }
                $i=0;


                echo '<div class="summary">Showing <b>1-' . count($dataProvider->models) . '</b> of <b>' . $dataProvider->pagination->totalCount . '</b> items.</div>';
                echo '<pre>';
                print_r($dataProvider->getPagination());
                echo '</pre>';

                echo \yii\widgets\LinkPager::widget([
                    'pagination' => $dataProvider->getPagination(),
                ]);


                foreach ($dataProvider->models as $model) {
                    echo '<tr>';
                    echo '<td><a href="/web/cases/view&id='. $model->case_id .'">' . $model->name . '</a></td>';
                    echo '<td>' . $model->getCitation() . '</td>';
                    echo ($model->bailli != "Not available" ? '<td><a class="external-link" href=' . $model->bailli . ' target="_blank">Visit site</a></td>' : '<td></td>');
                    echo ($model->adjudication_URL != "Not available" ? '<td><a class="external-link" href=' . $model->adjudication_URL . ' target="_blank">Visit site</a></td>' : '<td></td>');
                    echo '<td><a href="/web/cases/view?id='. $model->case_id .'#paragraph">' . $chapters[$i] .'</a></td>';
                    echo '</tr>';
                    $i++;
                }
            } ?>
        </table>

Controller:

public function actionIndex()
{

    if(Yii::$app->user->identity->approved !== 1) {
        return $this->redirect(['site/notapproved']);
    }

    $model = new Cases;
    $searchModel = new CaseSearch();// This is the data to be displayed in gridview
    $searchModelChapter = new CaseChapter;// This for when searching with categories
    //$test1 = $test->find()->asArray()->all();
    $mainCategory = Category::find()->all();// This is grabing the first category list to populate first drop down
    $subCategory = Subcategory::find()->all();
    $childCategory = Childcategory::find()->all();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);// This is the filter options for the gridview

    $page = 50;
    if (null !== Yii::$app->request->get('pageSize')) {
        $page = Yii::$app->request->get('pageSize');
        if ($page == "all") {
            $page = 10000;
        }
    }

    $dataProvider->pagination->pageSize=$page;
    $dataProvider->sort = array('defaultOrder' => [
        'name' => SORT_ASC
    ]);
    $request = Yii::$app->request;
    $post = $request->post();//$post now takes place of normal $_POST array;
    $get = $request->get();


    /*
    /*
     * This deals with the post values when the drop down lists are chosen and submitted via POST
     */
    if($searchModel->load(Yii::$app->request->post())){

        //The first category will always be set
        if(isset($post['CaseSearch']['category'])){
            if(isset($post['CaseSearch']['subcategory']) && !isset($post['CaseSearch']['childcategory'])){
                $childCategories = new Childcategory;
                $childcategories = $childCategories->findChildcategoryBySecond($post['CaseSearch']['subcategory']);
                $childcategories = $childCategories->findChildcategoryIds($childcategories);
                $dataProvider = $searchModelChapter->findChapters($childcategories);
                $category = null;
                $firstcategory = null;
                $subcategory_id = $post['CaseSearch']['subcategory'];
                $sub_case_chapter = new CaseChapter;
                $subcategory = $sub_case_chapter->findChapterSubCategories($subcategory_id);
            }

            if(!isset($post['CaseSearch']['childcategory']) && !isset($post['CaseSearch']['subcategory'])){
                $childCategories = new Childcategory;
                $childcategories = $childCategories->findChildcategoryByFirst($post['CaseSearch']['category']);
                $childcategories = $childCategories->findChildcategoryIds($childcategories);
                $dataProvider = $searchModelChapter->findChapters($childcategories);
                $category = null;
                $subcategory = null;
                $firstcategory_id = $post['CaseSearch']['category'];
                $first_case_chapter = new CaseChapter;
                $firstcategory = $first_case_chapter->findChapterFirstCategories($firstcategory_id);
            }

            if(isset($post['CaseSearch']['childcategory'])){
                $childcategory_id = $post['CaseSearch']['childcategory'];
            }else{
                $childcategory_id = null;
            }

            if($childcategory_id !== null){
                $dataProvider = $searchModelChapter->findChapters($childcategory_id);
                $case_chapter = new CaseChapter;
                $category = $case_chapter->findChapterCategories($childcategory_id);
                $subcategory = null;
                $firstcategory = null;
            }

        }elseif(isset($post['CaseSearch']['childcategory']) && !isset($CaseSearch['CaseSearch']['category']) && !isset($CaseSearch['CaseSearch']['subcategory'])){
            $searchModelChapter = new CaseChapter;
            $dataProvider = $searchModelChapter->findChapters($post['CaseSearch']['childcategory']);
            $category = $searchModelChapter->findChapterCategories($post['CaseSearch']['childcategory']);
            $subcategory = null;
            $firstcategory = null;
        }elseif(isset($post['CaseSearch']['subcategory']) && !isset($CaseSearch['CaseSearch']['category']) && !isset($CaseSearch['CaseSearch']['childcategory'])){
            $searchModelChapter = new CaseChapter;
            $childcat = new Childcategory;
            $categories = $childcat->findChildcategoryBySecond($post['CaseSearch']['subcategory']);
            $category_ids = $childcat->findChildcategoryIds($categories);
            $dataProvider = $searchModelChapter->findChapters($category_ids);
            $category = null;
            $subcategory = $searchModelChapter->findChapterCategories($post['CaseSearch']['subcategory']);
            $firstcategory = null;
        }else{
            return $this->render('index', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
                'mainCategory' => $mainCategory,
                'subcategory' => $subCategory,// to supply all subcategories in drop down list
                'childcategory' => $childCategory,// to supply all childcategories in drop down list
                'model' => $model,
            ]);
        }


        $dataProvider[0]->pagination->pageSize=50;

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider[0],
            'mainCategory' => $mainCategory,
            'chapters' => $dataProvider[1],
            'category' => $category,
            'subcategory' => $subcategory,
            'firstcategory' => $firstcategory,
            'subcategory' => $subCategory,// to supply all subcategories in drop down list
            'childcategory' => $childCategory,// to supply all childcategories in drop down list
        ]); 
    }
    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'mainCategory' => $mainCategory,
        'subcategory' => $subCategory,
        'childcategory' => $childCategory,
        'model' => $model,
    ]);

}
0

There are 0 answers