Yii2 Gridview get all selected row for all pagination

天涯浪子 提交于 2019-12-23 21:05:18

问题


I wrapped my gridview with Pjax widget like this

 \yii\widgets\Pjax::begin();
    gridview
 \yii\widgets\Pjax::end(); 

in order to make the gridview make ajax request when I click on each pagination.

I also use ['class' => 'yii\grid\CheckboxColumn'], in column as well. and I find that when I'm on first pagination I checked some rows and then go to second page and check some rows but when I go back to first page what I've checked is gone. My question is how can I keep all checkedrow for all pagination


回答1:


With current conditions (Pjax, multiple pages, yii\grid\CheckboxColumn) it's impossible because of the way it works.

When you click on the pagination links all GridView html content is replaced by new one that comes from the AJAX response.

So obviously all selected checkboxes on the previous page are gone.

Few possible ways to solve that:

1) Write custom javascript and server side logic.

As one of the options, you can send AJAX request to server with parameter meaning that user has chosen to select all data for the bulk delete operation (or use separate controller action for bulk deletion). In this case actually we don't need to get the selected data from user, because we can simply get them from database (credits - Seng).

2) Increase number of displayed rows per page.

3) Use infinite scroll extension, for example this.

4) Break desired action in several iterations:

  • select needed rows on first page, do action (for example, delete).
  • repeat this again for other pages.

You can get selected rows like that:

$('#your-grid-view').yiiGridView('getSelectedRows');



回答2:


I just solved this problem and it works properly with Pjax.

You may use my CheckboxColumn. I hope this can help. The checked items are recorded with cookies.

You can read the part with //add by hezll to understand how to fix it, because I didn't provide a complete general one.

Hope it works for you.

https://owncloud.xiwangkt.com/index.php/s/dGH3fezC5MGCx4H



来源:https://stackoverflow.com/questions/30066305/yii2-gridview-get-all-selected-row-for-all-pagination

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!