Search code examples
yii2yii2-advanced-appyii2-model

Kartik GridView Yii2 : Toggle data not working in toolbar


I am using Yii2 kartik gridview. Below are the 2 images before and after toggleData button is clicked. I am unable to see Full data in gridview.

BEFORE

enter image description here

AFTER

enter image description here

Only the pagination goes away but gridview does not display all records. I have 10 records for now, I have set pagination to 5 records. So If I click on "All", I must get all 10 records, but as you see I only get 5 records.

Below is code for ActiveDataProvider before rendering page.

CODE INSIDE CONTROLLER ACTION

 $searchModel = new StudentsAdminSearch();
        $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
        return \Yii::$app->controller->render('student/student_list', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);

CODE INSIDE StudentAdminSearch

$dataProvider = new ActiveDataProvider([
            'pagination' => [
                'pageSize' => Constants::$PAGE_SIZE, // $PAGE_SIZE=5
            ],
            'query' => $query,
        ]); 

Solution

  • I did something else, much easy and reliable than session variable.

    If you see, when i first land on list page I get a link in address bar like,

    http://localhost/nse/backend/web/index.php?r=site%2Fstudent-list
    

    When I click 'ALL' button as toggleButton inside Kartik GridView, I get a link in address bar like,

    http://localhost/nse/backend/web/index.php?r=site%2Fstudent-list&_tog1149016d=all
    

    If you see carefully I get extra parameter added as _tog1149016d with value "all". With different installations ans servers the number with '_tog' changes. So, Inside Search Model's search Method I did Something like this,

    $pagination = Utility::getPagination($params);
    
        $dataProvider = new ActiveDataProvider([
            'pagination' => $pagination,
            'query' => $query,
        ]);
    

    I wrote a small utility function to check request param whether it contains "all" or "page" or nothing.

    public static function getPagination($request_params){
        $param_val = 'page';
        foreach($request_params as $key => $value){
            if (strpos($key, '_tog') !== false) {
                $param_val = $value;
            }
        }
        $pagination = array();
        if($param_val == 'all'){ //returns empty array, which will show all data.
            return $pagination;
        }else if($param_val == 'page'){ //return pageSize as 5
            $pagination = ['pageSize' => 5];
            return $pagination;
        }
        return $pagination;  // returns empty array again.
    }
    

    Above code Works like charm. I am attaching proof of implementation and running scenarios below,

    ON CLICKING "Page" (It enables pagination, my case, it is 5)

    enter image description here

    ON CLICKING "All" (It populates all data)

    enter image description here