在Yii CLinkPager第一页链接中显示“page = 1”参数

时间:2011-07-22 09:29:02

标签: yii

我想创建一个记住搜索表单输入的CListView。我的应用程序将每个搜索输入(以及用户查看的当前页面)保存到用户会话中,并将搜索ID添加到分页参数中。

$pagination = array(
   'pageVar' => 'page',
   'currentPage' => $currentPage - 1,
   'params' => array(
         'search' => $searchId,
   )
);

但是由于CLinkPager的第一页链接没有显示'page = 1'参数,每次我点击其他页面的链接(比如第2页)时,应用程序会认为我需要当前的'搜索'(其当前页面值为2)。

要重新解释这个问题,有没有办法让CLinkPager在第一页链接显示param'page = 1'?或者你有另外一个处理这个问题的建议:)

谢谢

被修改

我的整体代码如下。如果您想提供更好的建议,请执行以下操作:)

public function actionIndex() {

            $model = new User();
            if (isset($_POST['User'])) {
                    $model->setAttributes($_POST['User']);
            } else {
                    if (isset($_GET['search'])) {
                            $searchId = $_GET['search'];
                            //Just getting a variable stored in the session
                            //array(
                            //     $searchId => array(
                            //          'model' => $model //the search model
                            //          'pageSize' => $pageSize //current pagination page size
                            //          'currentPage' => $currentPage //the search model
                            //     )
                            //);
                            $search = Yii::app()->user->getSearch($searchId); 
                            $model->setAttributes($search['model'], false);
                            $currentPage = $search['currentPage'];
                            $pageSize = $search['pageSize'];
                    } else {
                            //create default search
                    }
            }

            //Managing pageSize parameter
            if (isset($_GET['pageSize'])) { //Check whether there is pageSize in $_GET parameters
                    if (in_array($_GET['pageSize'], $this->getPageSizes())) {
                            $pageSize = $_GET['pageSize'];
                    } else {
                            $pageSize = self::DEFAULT_SEARCH_PAGESIZE;
                    }
            } else if (!isset($pageSize)) { //Check whether pageSize is already defined before
                    $pageSize = self::DEFAULT_SEARCH_PAGESIZE;
            }
            //Managing currentPage parameter
            if (isset($_GET['page'])) { //Check whether there is page parameter in $_GET parameter 
                    $currentPage = $_GET['page'];
            } else if (!isset($currentPage)) { //Check whether page is already defined before
                    $currentPage = self::DEFAULT_CURRENT_PAGE;
            }

            //Saving all into new session
            $newSearch = array(
                    'model' => $model->getAttributes(),
                    'currentPage' => $currentPage,
                    'pageSize' => $pageSize,
            );
            //Check whether if there is a search before and if it exists check whether the two search is the same
            if (!isset($search) || (serialize($search) != serialize($newSearch))) {
                    //Saves into session
                    $searchId = Yii::app()->user->saveSearch($newSearch);
            } //else use the old search id

            //-----------------------------------------------------
            //Building the search
            //Setting criteria
            $criteria = new CDbCriteria(array(
                                    'select' => array(
                                            '*', //just to simplify things
                                    ),
                            ));


            //Appending criteria to search model used
            $criteria = $model->search($criteria);

            //Appending pagination to current variables
            $pagination = array(
                    'pageSize' => $pageSize,
                    'pageVar' => 'page',
                    'currentPage' => $currentPage - 1,
                    'params' => array(
                            'search' => $searchId, //include the search ID in the link pager request URL
                    )
            );

            //Creating dataProvider for the search
            $dataProvider = new CActiveDataProvider('Property', array(
                                    'criteria' => $criteria,
                                    'pagination' => $pagination,
                            ));
            //Rendering
            $this->render('index', array(
                    'dataProvider' => $dataProvider,
                    'pageSize' => $pageSize,
                    'model' => $model,
                    'searchId' => $searchId,
            ));
}

1 个答案:

答案 0 :(得分:2)

您可以通过修改CPagination方法扩展createPageUrl并删除/更改if($page>0)条件,使其始终为true。 (或者更好的是,添加一个额外的参数来打开/关闭该行为。)