0

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,
        ]); 
Ankur Soni
  • 5,725
  • 5
  • 50
  • 81

3 Answers3

1

As your Active data provider is having the limit to show only 5 limit which is constant every time you get the data you will get 5 records only so you can use the yii2 session to set the data on click of toggle some thing like this

    $size=\Yii::$app->session->get('user.size');
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => [
            'pageSize' => isset($size) ? $size : 5,
        ],
        'sort' => [
            'defaultOrder' => [
                'id' => SORT_DESC,
            ]
        ],
    ]);

where user.size is the session value want or u can also remove the pagination size to get all data if size is not there

$notshowsize=\Yii::$app->session->get('user.size');
  $pagination = array();
if($notshowsize){
 $pagination = array();
 }else{
   $pagination = ['pageSize' => isset($size) ? $size : 5]
 }
$dataProvider = new ActiveDataProvider([
    'query' => $query,
    'pagination' => $pagination,
    'sort' => [
        'defaultOrder' => [
            'id' => SORT_DESC,
        ]
    ],
]);

where variable $notshowsize is boolean set to show data in pagination or not

  • how to write code in toggle button?? I mean 'toolbar' => [ '{toggleData}', '{export}' ] So where do I need to write the above code only for toggledata? – Ankur Soni Mar 09 '17 at 09:28
1

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

Ankur Soni
  • 5,725
  • 5
  • 50
  • 81
0

I was getting same problem, added a static function as following like improved version of above.

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.
        $pagination = ['pageSize' => false];

    }else if($param_val == 'page'){ //return pageSize as 5
        $pagination = ['pageSize' => 20];

    }else{
        $pagination = ['pageSize' => false];

    }
    return $pagination;  // returns empty array again.
}