简体   繁体   中英

Yii2: Get selected rows data from gridView checkbox columns into controller

I've view page( index.php ) in my Yii2 project, and I'm using Kartik gridView for showing the data

This the view from index.php:

在此输入图像描述

On the right side of view, I've a checkbox column. And I've an Export button. I want to export the selected name (selected by checkbox) into name.txt file.

I've finally make the export function, but I don't know how to get the selected data from view into controller.

I've try suggestions that I got from many forums, for example:

I put this javascript code in my view index.php :

<script>
function getRows(){
    var keys = $('#grid').yiiGridView('getSelectedRows');
    $.post({
        url: FakturOutController / exportAction,
        dataType: 'json',
        data: {keylist: keys},
        success: function(data) {
            alert('I did it! Processed checked rows.')
        },
    });
}

and set the export button like this:

<p>
    <button type="button" onclick="getRows()" class="btn btn-success">Export</button>
</p>

But I got nothing, the button didn't showed any action/reaction when clicked.

This is the gridView code in index.php:

`<?php Pjax::begin(); ?>
<?=
GridView::widget([
    'dataProvider' => $dataProvider,
    'tableOptions' => ['class' => 'table table-hover'],
    'columns' => [
        ['class' => 'yii\grid\SerialColumn',
            'header' => 'No',
        ],
        [
            'label' => 'Name',
            'value' => function($data) {
            return $data->name;
    }
        ],
        ['class' => '\kartik\grid\CheckboxColumn'],
    ],
    'toolbar' => [
        ['content' =>
            Html::a('<i class="glyphicon glyphicon-repeat"></i>', ['index'], ['data-pjax' => false, 'class' => 'btn btn-default', 'title' => 'Reset Grid'])
        ],
        '{export}',
        '{toggleData}'
    ],
    'panel' => [
        'heading' => '<i class="glyphicon glyphicon-align-left"></i>&nbsp;&nbsp;<b>Data</b>',
        'before' => '', //IMPORTANT
    ],
]);
?>
<?php Pjax::end(); ?>

<?=
    Html::a('<i class=" glyphicon glyphicon-export"></i> Export', ['export', 'userId' => $userId], ['class' => 'btn btn-success']);
?>`

Any help would be appreciated. Thanks

By inspect element on checkbox column you can find name of row ( checkbox name ). it contain id as value .

from that you can find how many rows are selected.

in my case i get ' selection []' in checkbox name.

ex.

<input type="checkbox" class="kv-row-checkbox" name="selection[]" value="1">

i write jquery code to get selected rows below.

<script>

    function getRows()
    {
        var strvalue = "";
        $('input[name="selection[]"]:checked').each(function() {
            if(strvalue!="")
                strvalue = strvalue + ","+this.value;
            else
                strvalue = this.value;
        });
     // strvalue contain selected row by comma separated      
    $.post({
        url: FakturOutController / exportAction,
        dataType: 'json',
        data: {keylist: keys},
        success: function(data) {
            alert('I did it! Processed checked rows.')
        },


       });
    }
    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM