yii 2分页正在丢失模型

时间:2015-06-09 13:31:43

标签: yii pagination yii2

是的,我有一个列表视图,显示我的数据库中的内容。这种分页效果很好。但是,只要我将过滤器应用于我的数据提供程序并尝试分页到第二页,它就会忘记所有过滤器并转到所有内容的第二页。

有没有人经历过这个?

如果您需要代码,请询问我会提供,但这将是一个很大的经历

查看:

        <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,
    ]);

}

0 个答案:

没有答案