6

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:

enter image description here

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

Community
  • 1
  • 1
Blackjack
  • 1,016
  • 1
  • 20
  • 51

1 Answers1

1

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>
Yasin Patel
  • 5,624
  • 8
  • 31
  • 53
  • Thanks, when I comment the code inside `$.post({})`, and add alert(strvalue), it's work add printout the value. But when I uncomment the `$.post({})`, the button didn't work. And I also edit the `url: FakturOutController / exportAction,` url: 'faktur-out/export', but it also didn't work. – Blackjack Jan 05 '17 at 03:04
  • thanks :) The case has been solved and I've been update the question :) – Blackjack Jan 05 '17 at 04:16
  • How to call getRows() from view? Can anyone explain. – Nivetha Jaishankar Feb 09 '18 at 03:21
  • can you please look into one of my [question](https://stackoverflow.com/questions/56196185/unable-to-post-the-data-from-view-to-controller-in-yii2) related to it ? – Moeez May 18 '19 at 05:43