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

4
  • May be onclick="getRows()" instead of onclick="displayResult()" will work. Commented Jan 4, 2017 at 12:27
  • @InsaneSkull Sorry that was a mistake, I've copy wrong button code, but I've update the code on the question, thanks :) Commented Jan 4, 2017 at 12:54
  • and code for gridview? Commented Jan 4, 2017 at 13:21
  • @InsaneSkull the code for gridView has been added, check it out :) Commented Jan 4, 2017 at 16:03

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

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.
thanks :) The case has been solved and I've been update the question :)
How to call getRows() from view? Can anyone explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.