yii 2分页正在丢失模型中的筛选器数据

yii 2分页正在丢失模型中的筛选器数据,yii,pagination,yii2,Yii,Pagination,Yii2,对,我有一个列表视图,显示我数据库中的内容。分页可以很好地处理这个问题。但是,当我对数据提供者应用过滤器并尝试分页到第二页(例如)时,它会忘记所有过滤器并转到所有内容的第二页 有人经历过吗 如果你需要代码,请一定要问,我会提供,但这将是一个地狱很多去通过 视图: 不幸的是,您应该提供代码。@robsch我已经用代码更新了。找到答案需要全部代码吗?你能减少它吗?@robsch我现在已经编辑了太多代码了。我想知道谁会有兴趣阅读这篇文章,并了解你的代码实际上做了什么。您应该大幅度减少它,并提供更详细的

对,我有一个列表视图,显示我数据库中的内容。分页可以很好地处理这个问题。但是,当我对数据提供者应用过滤器并尝试分页到第二页(例如)时,它会忘记所有过滤器并转到所有内容的第二页

有人经历过吗

如果你需要代码,请一定要问,我会提供,但这将是一个地狱很多去通过

视图:


不幸的是,您应该提供代码。@robsch我已经用代码更新了。找到答案需要全部代码吗?你能减少它吗?@robsch我现在已经编辑了太多代码了。我想知道谁会有兴趣阅读这篇文章,并了解你的代码实际上做了什么。您应该大幅度减少它,并提供更详细的信息(用户输入什么、生成哪个URL、使用哪个if/else分支…)。顺便说一句,我认为在一个动作中有这么多代码是不好的——我认为你也应该改进这一点。
        <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>
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,
    ]);

}