7

I wants to know if there is any inbuilt option in yii2 gridview checkbox column to select/deselect all rows at once..

For example if i have 500 records in a gridview and i am displaying 100 records at once than i can only select 100 records at once and do any bulk action.

I want user to be able to select all 500 records at once and do any bulk action with those record at once.. hope you all get my question

I made a suggestion to yii framework owners on github and they have confirmed that they will do this enhancement soon enough and put this issue in enhancement section, so hoping that they will include it soon enough but until then guide me to achieve this in any alternate way

Thank you

Mike Ross
  • 2,942
  • 5
  • 49
  • 101

3 Answers3

2

Yes, You can use CheckBoxColumn. Just add the following lines into your GridView's columns array:

[
     'class' => 'yii\grid\CheckboxColumn',
     'name' => 'id'
],

As Yii2's Official document:

CheckboxColumn displays a column of checkboxes in a grid view. To add a CheckboxColumn to the yii\grid\GridView, add it to the columns configuration as follows:

'columns' => [
// ...
    [
        'class' => 'yii\grid\CheckboxColumn',
        // you may configure additional properties here
    ],
]

Users may click on the checkboxes to select rows of the grid. The selected rows may be obtained by calling the following JavaScript code:

var keys = $('#grid').yiiGridView('getSelectedRows');
// keys is an array consisting of the keys associated with the selected rows
Ali MasudianPour
  • 14,329
  • 3
  • 60
  • 62
  • i have already implemented that.. but my problem is little different.. Consider that i have 500 records in the grid.. and i am displaying only 100 row a page.. And if i have to select all 500 rows at once and do the common action for all 500 rows than i cant do that from very first page... I hope you get my point – Mike Ross Oct 19 '15 at 00:18
  • @MikeRoss You please check this answer https://stackoverflow.com/a/30066826/959939 – SenG Feb 23 '18 at 03:47
0

You have to "save" the selected grid items on every pagination by doing a post request when pagination is used (serialize the form and doing an ajax request). Then use the submitted id's to have some pre-selection part on your page.

In this pre-selection part render some checkboxes (checkbox group) to the page with the preselected id's which aren't on your current grid page and a simple text to indicate which item it is (name etc.).

So on your current grid page you select the items with the grid checkboxes and when you paginate those "selected" checkboxes are rendered in this pre-selection part.

Whith this you can select items over multiple pages and could also deselect items which aren't on the page.

On your "action" then you just have to combine both checkbox groups to get all selected items.

there is something similar for yii1 mentioned here

there is also a yii1 extension which does this here maybe you will replicate the code in yii2 or find some similar extension.

Community
  • 1
  • 1
BHoft
  • 1,663
  • 11
  • 18
0

By default, it is not possible (yet). But you can save the keys for later. Here is the basic idea, I hope it gives you a start.

<script type="text/javascript">
$(document).ready(function (){
    var selectedItems = [];

    $('#delete_selected_items_btn').click(function (){
        selectedItems = selectedItems.concat($('.grid-view').yiiGridView('getSelectedRows'));
        // select all rows on page 1, go to page 2 and select all rows.
        // All rows on page 1 and 2 will be selected.
        console.log(selectedItems); 
    })
})
</script>

My assumption is that you are using Pjax for the gridview.

Prabowo Murti
  • 1,216
  • 2
  • 18
  • 27
  • thank you for answering but for now i created extra action which accepts `eventid` and i find all keys for that event and send it from separate action. – Mike Ross May 06 '16 at 01:59